2011-03-30 47 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