Nhibernate QueryOver API

This forum is where we'll mirror posts that are of value to the community so they may be more easily found.
Post Reply
User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Nhibernate QueryOver API

Post by mazhar » Thu Jun 12, 2014 2:25 am

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.

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Nhibernate QueryOver API

Post by jmestep » Thu Jun 12, 2014 3:56 am

Thanks, mazhar. I had noticed it in R8 so it will be interesting using it.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Nhibernate QueryOver API

Post by mazhar » Thu Jun 12, 2014 4:00 am

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.

Post Reply