nHibernate mapping files no more

During an evenings coding, I ran into a typical problem with an nhibernate mapping file, (it was a spelling mistake) I had one of those moments when you go onto google to see if any one has come up with something smart to tackle this. I design from the the domain model  first so i was not looking for a code generator that you point at a database and way it goes. I was looking for a OO approach to this that removed xml from the mix.

It was a successful google search, I came across Fluent-NHibernate.

This sample code snippet was enough to stop my search and get me excited.

public CustomerMap : ClassMap<Customer>
{
  public CustomerMap()
  {
    Id(x => x.ID);
    Map(x => x.Name);
    Map(x => x.Credit);
    HasMany<Product>(x => x.Products)
      .AsBag();
    Component<Address>(x => x.Address, m =>
    {
        m.Map(x => x.AddressLine1);
        m.Map(x => x.AddressLine2);
        m.Map(x => x.CityName);
        m.Map(x => x.CountryName);
    });
}

What appeals to me about this code above:

  • The fluent interface and the use of lambda’s makes it very readable.
  • As its a class, I can group all my map objects into a single assembly. Then let the concerning layer in my application reference it, keeping my domain model persistent ignorant. (Before I came across “Fluent nHibernate” I was going to resort to using attributes)
  • Thinking ahead, when/if I refactor my entities, resharper will also make changes to map objects.
  • I am a TDD/BDD practitioner, Its an object, i want to put my mapping logic under test.
  • Lastly, no need for xml mapping files any more.

So I read some more, downloaded the code and had a play.

You can get an idea of how this works in about 5 minutes by running the quick start. The instructions and general documentation on this site is a little light on the ground and not much more info on the web at the moment to give more depth on the usage of this API.  The quick start uses a Cat object as the entity to persist.  I don’t really come across to many cats, dogs or other animals in programming so I wanted to used a real life example to get a better feel for it. At the moment to get assemblies, you need to checkout the code from the svn truck and compile the code yourself.

Running the Quick start

To aid the quick start instructions

  1. Create a new folder on PC and checkout the code from the SVN trunk “http://code.google.com/p/fluent-nhibernate/source/checkout&#8221;.
  2. From your working folder. Open up the quickStart solution and build it.
  3. As the context is ORM you need a database to push and pull data from. I had an instance of sql express to hand, so I created a blank database called “test”.
  4. Back to Visual studio, in the “FluentNNibernate.QuickStart” project, change the connection string in the app.config to point to your database.
  5. The quick start does not have an UI to use, but its behavior is expressed in the integration tests. Open up “QuickStartTester.cs”, find the “Export_Schema” method (which is decorated with the Explicit nunit attribute) and run the test (I used Test driven .net to run the test). This will create a new table in your database.
  6. Now run the “Can_Persist_Cat” test method. This tests creates an instance of Cat, populates it and saves, it. Lastly the test, pulls the data from the database by the id and compares the ids.

This is a very simple test but enough to wet my appetite. I have a working example using more business like entities on this post here Implementing Fluent-nHibernate.

Advertisements

One response to “nHibernate mapping files no more

  1. Pingback: Implementing Fluent-nHibernate « Russell East’s blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s