Getting started with NHibernate

Monday, 03 March 2008

Posted by Sébastien Lachance with Comments (0)

Without digging in too much detail about NHibernate, I will presume you are familiar with what this O/R Mapper can do.

The application configuration file :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="hibernate.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="hibernate.connection.connection_string">Server=.SQLEXPRESS;Initial Catalog=RecipeHelperTest;User Id=recipeuser;Password=recipeuser;</property>
<mapping assembly="RecipeHelper.Domain" />
</session-factory>
</hibernate-configuration>
</configuration>

This is standard stuff that you can find in the documentation. One interesting point however is the mapping. You can specify where your domain object to be persisted are. This will allow you to not worry about specifying them in code.

Creating a mapping file :

To be able to persist your entities, you'll need to create a mapping file. In my opinion, this is the hardest thing to learn about NHibernate. But don't worry, it is well documented.

The strategy I am using is one file per entities, in the same assembly. I also use the same name with the extension ".hbm.xml". The trickiest part is that you need to set the Build Action to "Embedded Resource". Once all this is completed you can now start writing your mapping information :

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="RecipeHelper.Domain" namespace="RecipeHelper.Domain" default-lazy="false">
<class name="Recipe" table="Recipes" >
<id name="ID" column="ID" type="int" access="field.lowercase-underscore">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" not-null="false"/>
<property name="Reference" column="Reference" type="string" not-null="false" />
<property name="Rating" column="Rating" type="int" not-null="false" />
</class>
</hibernate-mapping>

Preparation for using NHibernate :

The biggest performance hit is the creation of the ISessionFactory. For this reason, it should only be instantiated once. I created a singleton that will allow me to have access to a single session factory.

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace RecipeHelper.Domain
{
public class NHibernateConfig
{
private static ISessionFactory _sessionFactory;

static NHibernateConfig()
{
Configuration config = new Configuration();

_sessionFactory = config.Configure().BuildSessionFactory();
}

public static ISessionFactory GetSessionFactory()
{
return _sessionFactory;
}

}
}

Conclusion :

This isn't a complete tutorial to get started with NHibernate but should cover the essential steps to prepare your project to use it.




blog comments powered by Disqus