NHibernate3 Query vs QueryOver [duplicate]
Asked Answered
R

2

28

I noticed there are two ways to create nice generic friendly access to nhibernate.

IQueryOver<T, T> query= session.QueryOver<T>().Where(criteria);

and

IQueryable<T> query= session.Query<T>().Where(criteria);

Implementations of each interface.

IQueryOver<TRoot, TSubType> : IQueryOver<TRoot>, IQueryOver

and

IQueryable<out T> : IEnumerable<T>, IQueryable, IEnumerable

IQueryable implements IEnumerable, thus supports all the LINQ friendly things you would expect. I am tending towards this implementation, but was wondering if anyone knew what the purpose of QueryOver was that you cannot accomplish with Query?

Rame answered 16/3, 2011 at 16:39 Comment(1)
Yes, you are correct. Though there isn't a great amount of detail on the trade-offs.Rame
P
31

QueryOver combines extension methods and lambda expressions:

IList<Cat> cats =
    session.QueryOver<Cat>()
        .Where(c => c.Name == "Max")
        .List();

QueryOver is a strongly-typed querying technology built on top of NHibernate’s Criteria API.

You can read more info here and here.

As far as I know some features in the linq provider are not implemented yet.
I would use QueryOver.
It allows you to write elegant code and it is fully featured.

Something worth reading.

Palingenesis answered 16/3, 2011 at 16:50 Comment(0)
V
16

QueryOver syntax is NHibernate specific, thus it has many powerful methods that you just can't match in LINQ.

As LeftyX said, the LINQ implementation for NH is not complete, and I've had several headaches with it. For example, recently I had problems using the 2nd level cache, the Future values, and NH Spatial extensions with LINQ, all due to an incomplete implementation or bugs (and not mentioning the performance of some generated SQL, which is sometimes pretty awful).

In all these cases I had to use QueryOver, and after surpassing the learning-curve, is has, IMHO, a much nicer syntax than LINQ.

But LINQ via Query also has advantages; like being ORM agnostic (which might leverage a cleaner repository architecture), and for simple queries it is more than enough.

Vincenty answered 17/3, 2011 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.