-
Get a monthly update on best practices for delivering successful software.

Fluent NHibernate is an extension of the widely used and very popular NHibernate framework for Microsoft .NET. It is an open source framework that sits on top of the NHibernate layer and utilises all the core NHibernate methods. This framework provides an alternative to the standard XML based mappings (.hbm xml files) of NHibernate. It lets you define the NHibernate mappings in strongly typed and concise C# code. For those who are new to NHibernate, here is more information.
Traditional NHibernate XML mapping
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QuickStart" assembly="QuickStart"> <class name="Cat" table="Cat"> <id name="Id"> <generator class="identity" /> </id> <property name="Name"> <column name="Name" length="16" not-null="true" /> </property> <property name="Sex" /> <many-to-one name="Mate" /> <bag name="Kittens"> <key column="mother_id"/> <one-to-many class="Cat"/> </bag> </class> </hibernate-mapping>
Fluent NHibernate equivalent
public class CatMap : ClassMap<Cat>
{
public CatMap()
{
Id(x => x.Id);
Map(x => x.Name)
.WithLengthOf(16)
.Not.Nullable();
Map(x => x.Sex);
References(x => x.Mate);
HasMany(x => x.Kittens);
}
}
Why use Fluent NHibernate? Here are some benefits that it offers:
I think this is a good place to start picking up Fluent. You find a nice little project example that talks about mapping a simple database schema and building an application to read data using the fluent mappings.
Related posts:
Topics: Data Mapper, Fluent, NHibernate, ORM
We have used FluentNHibernate on a project since the beginning of the year and it has worked great for us — much easier to manage than the old NHibernate XML config files. And, with tools like ReSharper for refactoring, keeping changes in the interface/classes paired with mapping classes is extremely easy.
Comment by Robert Hurlbut, Tuesday, July 14, 2009 @ 8:58 pm
@Robert. Very true. It is better than traditional NHibernate
Comment by Karthik, Wednesday, July 15, 2009 @ 1:53 pm