fluent-nhibernate for creating entity mapping files

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

Related posts

Comments

August 5. 2008 12:09 PM

Interesting, however I do wonder how easy it works in real life scenario's. Gonna give it a try Smile

JV

August 8. 2008 03:56 PM

Pingback from blog.jagregory.com

Introducing Fluent NHibernate — James Gregory

blog.jagregory.com

September 2. 2008 03:01 AM

Pingback from nhforge.org

A fluent interface to NHibernate - Part 1 - NHibernate blog - NH Forge

nhforge.org

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 18. 2008 09:07 PM

About Me

I'm a technologist living in Springdale, AR. My passion is learning and using technology from around the world.

Blog Twitter Flickr del.icio.us
TwitterCounter for @zachariahyoung

Flickr

Recent comments

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008