Screencast on Fluent NHibernate with Chad Myers

27. August 2008

On August 21, I hosted a LiveMeeting event with Chad Myers to talk about Fluent NHibernate.  Fluent NHibernate is a API that eliminates the messy XML files.  It will create the mapping files and configuration file.

In this demo, Chad will convert the Nhibernate Made Simple project on Code Project to use the Fluent NHibernate. 


kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 5.0 by 1 people

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

fluent-nhibernate, VAN

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

fluent-nhibernate for creating entity mapping files

1. August 2008

fluent-nhibernate is an API that creates nhibernate entity mapping files.  This will prevent you from having to create the XML mapping files and provides testability.  This API was part of the Shade Tree code base written by Jeremy Miller. 

To understand how to use this API you need to understand fluent interfaces.  This pattern is typically used for constructing complex objects.  Typically these complex objects are used when building XML or text files.  When building these complex objects you may be provided with lots of different options.  So instead of using a constructor, the object is constructed by what appears to be method chaining.  After each method has been called the object returns a reference to itself for the next method to call.  Using this pattern makes the coding more fluent and simpler to read.  Let's review a few lines of code to illustrate this concept.  One of the examples that I was provided was from Chad Myers.  His example is a “Lorem Ipsum” filler text generator. (http://lipsum.org).

This sample could be a typical object construction.

   1:  var generator = new LipsumGenerator();
   2:  generator.UnitType = UnitType.Pargraph;
   3:  generator.NumberOfUnits = 5;
   4:  generator.StartWithLorem = true;
   5:  generator.ColumnWidth = 76;
   6:  var text = generator.Generate();
 
But if you're using a fluent interface API. It might look like this.
   1:  var text = new LipsumGenerator()
   2:                       .Number OfParagraphs(5)
   3:                       .StartingWithLorem()
   4:                       .WithColumnWidth(76)
   5:                       .Generate();
 
 With fluent-nhibernate your code would look like following.
   1:  public CustomerMap : ClassMap<Customer>
   2:  {
   3:    public CustomerMap()
   4:    {
   5:      UseIdentityForKey(x => x.ID);
   6:      Map(x => x.Name);
   7:      Map(x => x.Credit);
   8:      HasMany<Product>(x => x.Products)
   9:        .AsBag();
  10:      Component<Address>(x => x.Address, m =>  
  11:      {  
  12:          m.Map(x => x.AddressLine1);  
  13:          m.Map(x => x.AddressLine2);  
  14:          m.Map(x => x.CityName);  
  15:          m.Map(x => x.CountryName);  
  16:      });
  17:  }
 

The following entity mapping file would be created.

   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
   3:      namespace="Eg" assembly="Eg">
   4:      <class name="Customer" table="Customers">
   5:          <id name="ID">
   6:              <generator class="identity" />
   7:          </id>
   8:          <property name="Name" />
   9:          <property name="Credit" />
  10:          <bag name="Products" table="Products">
  11:              <key column="CustomerID"/>
  12:              <one-to-many class="Eg.Product, Eg"/>
  13:          </bag>
  14:          <component name="Address" class="Eg.Address, Eg">
  15:              <property name="AddressLine1" />
  16:              <property name="AddressLine2" />
  17:              <property name="CityName" />
  18:              <property name="CountryName" />
  19:          </component>
  20:      </class>
  21:  </hibernate-mapping>

My links of fluent interfaces tagged on delicious.

http://delicious.com/zachariahyoung/fluentinterface

 

kick it on DotNetKicks.com

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 5.0 by 1 people

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

fluent-nhibernate