when to use which- CriteriaQueries vs QueryOver vs Named Queries vs LINQ to Nhibernate

1,394 views
Skip to first unread message

Vikram Nayak

unread,
Jan 13, 2011, 7:27:52 PM1/13/11
to nhusers
Are there any best practices / scenarios when one API is better than
the other?

Daniel Lidström

unread,
Jan 25, 2011, 10:12:29 AM1/25/11
to nhusers
On 14 Jan, 01:27, Vikram Nayak <vikram.na...@gmail.com> wrote:
> Are there any best practices / scenarios when one API is better than
> the other?

Here's what I've learnt:

* Use CriteriaQueries when you are letting user build queries
dynamically. Actually you have other options now, with Linq to
NHibernate you can do this with Specification Design Pattern.
* Use named queries for best performance. They will be initialized by
NHibernate. You can use them for frequent querying. You will use
Hibernate Query Language for this.
* Use Linq to NHibernate if you prefer type-safe query code. You can
also use L2Nh to implement the Specification Design Pattern.

I'm not sure when to use QueryOver. I keep forgetting which means it
is not that interesting to me :-)
Good luck!

P.S. For more about Specifications you can see this post:
http://huyrua.wordpress.com/2010/08/25/specification-pattern-in-entity-framework-4-revisited/

Daniel

Fran Knebels

unread,
Jan 25, 2011, 10:16:40 AM1/25/11
to nhu...@googlegroups.com
I believe QueryOver is just a type safe layer on top of Criteria.

http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

> --
> You received this message because you are subscribed to the Google Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
>
>

Jason Meckley

unread,
Jan 25, 2011, 11:23:12 AM1/25/11
to nhu...@googlegroups.com
HQL queries are useful when only the parameters of the query change.
Dynamic queries are useful when the query is built at runtime. for example if the selected fields, or criteria (where clause) changes. The most common example is a traditional search screen.

some prefer to use dynamic/linq queries all the time because R# can aid in refactoring. I haven't found named queries to be a problem, and prefer to use them when possible. it's easier to read sql/hql than criteria or linq.


Reply all
Reply to author
Forward
0 new messages