Does the Fluent NHibernate create static XML mapping files?

11. August 2008

This was one of the first questions I had when I started learning about the Fluent Nhibernate API.  In this article, I want to explain why the  API does not need to create static XML files.  This API can create static XML files, but it is not necessary.  If you have not already read Skinning the Cat with Fluent NHibernate by Bobby Johnson I would start there first. 

When using just NHibernate you can configure the ISessionFactory in several ways.  The following code is one way to configure the ISessionFactory instance.

   1:  ISessionFactory sessionFactory =
   2:        new Configuration().Configure().BuildSessionFactory();

This approach takes a full configuration file called hibernate.cfg.xml and builds the entire sessionFactory in one complete pass.  Within this configuration file is the locations of the mapping files.  During the creation of the instance the mapping files are parsed and loaded into the instance. 

The following XML is a sample hibernate.cfg.xml file.  Notice the mapping tags at the bottom.

   1:  <?xml version='1.0' encoding='utf-8'?>
   2:  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   3:      <!-- an ISessionFactory instance -->
   4:      <session-factory>
   5:          <!-- properties -->
   6:          <property name="connection.provider">NHibernate.
   7:                    Connection.DriverConnectionProvider</property>
   8:          <property name="connection.driver_class">NHibernate.
   9:                    Driver.SqlClientDriver</property>
  10:          <property name="connection.connection_string">Server=
  11:                    localhost;initial catalog=nhibernate;
  12:                    User Id=;Password=</property>
  13:          <property name="show_sql">false</property>
  14:          <property name="dialect">NHibernate.
  15:                    Dialect.MsSql2000Dialect</property>
  16:          <property name="use_outer_join">true</property>
  17:          <!-- mapping files -->
  18:          <mapping resource="NHibernate.Auction.Item.hbm.xml" 
  19:                           assembly="NHibernate.Auction" />
  20:          <mapping resource="NHibernate.Auction.Bid.hbm.xml" 
  21:                           assembly="NHibernate.Auction" />
  22:      </session-factory>
  23:  </hibernate-configuration>

With the Fluent NHibernate API, this process is handled differently.  The addMappingsFromAssembly() method in the PersistenceModel class of the API will use reflection to loop through all the types in the assembly, and see if any of them implement IMapping (ClassMap<T> implements IMapping). Next the types that are identified will be converted to in-memory XmlDocument objects.  These objects will then be passed into the configuration instead of the static XML mapping files.  Nhibernate has always supported XmlDocument.  The below code shows how the configuration is done with the Fluent NHibernate API. 

   1:  var cfg = new Configuration().Configure();
   2:   
   3:  var persistenceModel = new PersistenceModel(); 
   4:  persistenceModel.addMappingsFromAssembly
          (Assembly.Load("Your DAL Assembly")); 
   5:  persistenceModel.Configure(cfg); 
   6:   
   7:  ISessionFactory sessionFactory = cfg.BuildSessionFactory();

In my next post, I will explain how the Fluent NHibernate API can be used for testing the ClassMaps.

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

fluent-nhibernate

Comments