Read:
http://davybrion.com/blog/2009/12/using-nhibernate-in-your-service-layer/
– 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
I just found a good blog post about the may-to-many relationship in NHibernate.
The configuration sis done in Fluent NHibernate.
http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx
To continue my little "post with good links"… here is a good article by Ayende, explaining cascadin with NHibernate:
Here is the basics:
Here is what each cascade option means:
NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.
Update 18-11-2008, well it turs out there infect is a NHibernate 2.0documentation here:
http://nhforge.org/doc/nh/en/index.html
A very good place to start is here:
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/
Chapter 5 explains setting up properties in the mapping file, tags and attributes.
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/mapping.html
Chapter 6 is about collections and lists
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections.html
Handling many-to-many relations can be difficult, especially when it comes to cascading and data integrity. This blog post explains one way of handling it.