← Home

NHibernate config made simple

So, I use NHibernate a lot. I’ve found the following approach for NHibernate configs in my .config file to be especially useful:

      <section name="hibernate-configuration"
                  NHibernate" /> 

   <hibernate-configuration xmlns="urn:nhibernate-configuration-2.0"> 
         <property name="hibernate.dialect">
         <property name="hibernate.connection.provider">
         <property name="hibernate.connection.connection_string">
         <property name="hibernate.show_sql">
         <property name="hibernate.connection.driver_class">
         <property name="hibernate.connection.isolation">
         <property name="hibernate.use_proxy_validator">

         <mapping assembly="mobyProject" /> 

Basically, I use the NHibernate config section handler to initialize NHibernate’s session factory config values and to specify which assembly contains my mappings (using ). Since I keep all of my business objects and mapping files in one directory, this is especially helpful.

As you probably know, not all of the config settings above are needed, but I like to document ones I might want to change later, even if they’re just set to the default now… it helps jog a tired brain now and then.

Configuring your Session Factory is an extremely simple call then as well:

sessionFactory = new Configuration().Configure().BuildSessionFactory();