Page 1 of 1

Nhibernate QueryOver API

Posted: Thu Jun 12, 2014 2:25 am
by mazhar
You people are already working with Nhibernate criteria queries where we have to use magic strings to tell nhibernate about property names. Nhibernate version we have in gold does have another API called QueryOver API for compile time safe queries. You can make use of Visual Studio intellisense with QueryOver API while writing queries :) This comes with multiple advantages for example reduced coding time, no typos and no more broken queries if you change the name of some property but forgot to update the change in queries where name was hard coded. In Gold R8 I wrote few helper methods in NhibernateHelper class to help create QueryOver handle. Let me give you some quick examples

Code: Select all

// CRITERIA API
Product p = NHibernateHelper.CreateCriteria<Product>()
                    .Add(NHibernate.Criterion.Restrictions.Eq("Id", 1))
                    .UniqueResult<Product>();

// QUERYOVER API
Product p = NHibernateHelper.QueryOver<Product>()
                    .Where(p => p.Id == productId)
                    .SingleOrDefault<Product>();

You can see the lambda expression in where clause is making it compile time safe. You can learn more about QueryOver API here http://nhforge.org/blogs/nhibernate/arc ... h-3-0.aspx
Give it a try I believe you will love it. Specially the simple queries are super easy to write using QueryOver API due to lambda expressions and intellisense.

Re: Nhibernate QueryOver API

Posted: Thu Jun 12, 2014 3:56 am
by jmestep
Thanks, mazhar. I had noticed it in R8 so it will be interesting using it.

Re: Nhibernate QueryOver API

Posted: Thu Jun 12, 2014 4:00 am
by mazhar
jmestep wrote:Thanks, mazhar. I had noticed it in R8 so it will be interesting using it.
I see, you may have noticed future queries we used on some pages. That is a great feature of nhibernate which helps you pack/batch more then one queries in a single database hit. I will make a post explaining future queries and some performance tips with eager loading in nhibernate.