且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不能在NHibernate中将泛型与CreateCriteria一起使用?

更新时间:2022-11-23 13:19:49

CreateCriteria 方法要求您使用引用类型-在DAL方法上添加约束:

CreateCriteria method requires you to use reference types - add constraint on your DAL method:

public IList<Entity> GetAll<Entity>()
     where Entity : class
{
    return _session.CreateCriteria<Entity>().List<Entity>();
}

这自然意味着您传递给此方法的任何 Entity 类型都必须是引用类型.

This naturally implies that any Entity type you pass to this method must be a reference type.

我还建议仅命名通用类型参数 TEntity - Entity 有点令人困惑(因为这是实体基类的完美称呼).

I also suggest naming your generic type parameter TEntity - Entity alone is a bit confusing (as it's perfectly fine name for say, entity base class).