– Updated on the 2. January 2010
I have been using NHibernate for some time now, and I am very satisfied with the functionality it provides. However, I do really not like “magic strings” as these makes the code resistent to refactoring.
NHibernate makes use of “magic strings” when doing a query using HQL or the Criteria API. I tend to use the Criteria API as this, opposed to HQL, limits the logic encompassed in “magic strings”.
As I mostly use the Criteria API, I thought, why not do something about it
 So I did. Very quickly a Fluent Criteria interface was implemented. Here is how it is used:
var email = "email@email.com";
var customerType = CustomerType.Default;
var result = nhSession.Criteria<Customer>()
.Add.Equals(x => x.Email, email)
.Add.Equals(x => x.Type, customerType)
.Order.Desc(x => x.Created)
.NHCriteria
.SetMaxResults(1)
.UniqueResult<Customer>();
In this line we call the extension method Criteria with Customer as a generic argument:
var result = nhSession.Criteria<Customer>()
By doing so we of the alternate Fluent Criteria API I have implemented. Currently it is only some of the Restrictions and Order classes that have been implemented, but you are free to use these classes and extend them as you wish.
By calling the NHCriteria property, you get the normal NHibernate criteria API, so here you can set the cache options, max results, etc.
Download the source code here http://ovesen.codeplex.com/wikipage?title=NHibernateFluentCriteria&referringTitle=Home
Happy New Year btw