NHibernate Criteria Restriction vs Expression
Asked Answered
E

3

23

If I search for NHibernate Criteria API query examples in internet there are examples that use Restrictions and others use Expression. What are the differences between those two?

For example:

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

posts = session.CreateCriteria<Post>()
    .Add(Restrictions.Eq("Id", 1))
    .List<Post>();
Ehr answered 30/3, 2011 at 7:44 Comment(0)
A
18

I think Restrictions were released in NH2 and is now the favoured way.

According to Resharper whenever I use Expression I get a hint to say Access to a static member of a type via a derived type

Also according to this post by Ayende:-

Prefer to use the Restrictions instead of the Expression class for defining Criteria queries.

Arrio answered 30/3, 2011 at 8:3 Comment(0)
H
14

In the source code for namespace NHibernate.Criterion.Expression is says that "This class is semi-deprecated use Restrictions"

Hulbard answered 30/3, 2011 at 10:45 Comment(0)
S
1

Expression inherits from Restrictions but it is recommended to use Restrictions. Expression is apparently deprecated.

According to Ayende (old post about NH 2.0), documentation will usually refer to Restrictions.

Sherbrooke answered 30/3, 2011 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.