2011-03-30 65 views
21

如果我在互联网上搜索NHibernate Criteria API查询示例,有些示例使用限制,其他使用表达式。这两者之间有什么区别?NHibernate标准限制与表达式

例如:

posts = session.CreateCriteria<Post>() 
    .Add(Expression.Eq("Id", 1)) 
    .List<Post>(); 

posts = session.CreateCriteria<Post>() 
    .Add(Restrictions.Eq("Id", 1)) 
    .List<Post>(); 

回答

16

我认为限制在NH2发布,现在是最受欢迎的方式。

据ReSharper的,每当我使用Expression我收到一个提示说Access to a static member of a type via a derived type

而且according to this post由Ayende: -

喜欢使用的限制,而不是表达类的 定义 条件查询。

1

表达从限制继承但推荐使用的限制。表达式显然已被弃用。

根据Ayende (old post about NH 2.0),文档通常会引用限制条件。

12

的源代码namespace NHibernate.Criterion.Expression是说,“这个类是半弃用使用限制”

+0

1用于指向源代码。 – 2011-03-30 11:13:46