Fluently NHibernate

logo

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:

  • It favors convention over configuration and overcomes the repetitiveness that is there with NHibernate. For instance, NHibernate requires string properties to be not null and have a default value. You don't have to handle this if you go fluent.
  • By getting rid of XML based mappings, it paves way for easy-to-read code that can be refactored and maintained in a simpler way. The mappings sit along with your code so any refactorings/changes to the code will cause the mappings to be affected as well. This helps catch compile errors/bugs with mappings.
  • One other obvious benefit is that the XML verbosity of NHibernate can be avoided.

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:

  1. .NET 3.5 Linq to Sql and IBatis.NET
  2. Hibernate Gotcha: java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
  3. Afraid of LIE (LazyInitializationException)… Don’t Be.
  4. LINQ to My Domain
  5. HSE 1.1 – Hibernate, Spring and Echo2

Comments: 2 so far

  1. 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

  2. @Robert. Very true. It is better than traditional NHibernate

    Comment by Karthik, Wednesday, July 15, 2009 @ 1:53 pm

Leave a comment

Powered by WP Hashcash

Launch: Pathfinder Newsletter

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

    Subscribe via email


    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com