Adding Fluent NHibernate support for Castle Windsor’s NHibernate Integration 

Tags: .NET

We have used NHibernate as ORM mapper in many projects and to facilitate automatic session and transaction management we employed the Castle Windsor NHibernate Integration facility.

When using the Castle NHibernate Facility the configuration file for castle (castle.xml.config) looks like this:

image

Where the mapping assembly contains all the .hbm.xml files as embedded resources.

When the Fluent NHibernate framework appeared we started thinking about using it in our projects. But we wanted to do this with minimal changes to the Castle configuration schema (in the above screenshot).

The ideal situation would be to specify an assembly containing the Fluent NHibernate mapping classes instead of the assembly containing mapping xml files.

In the screenshot from above you can see in the  highlighted area an <assembly/> declaration. That assembly will be used by Castle NHibernate Facility’s default xml mapper.

We would like to make it possible to specify our own mapper which will make use of Fluent NHibernate to load the mapping classes from the specified assembly.

As the Fluent NHibernate framework is open-source, we considered making out custom build to add the needed functionality. Next I will describe the steps needed to implement and use the custom Fluent NHibernate mapper with Castle’s NHibernate Facility.

1. Download the necessary source files:

2. Create a new solution including the following projects:

  • Castle.Core
  • Castle.DynamicProxy
  • Castle.Facilities.NHibernateIntegration
  • Castle.MicroKernel
  • Castle.Services.Transaction
  • Castle.Windsor
  • FluentNHibernate
  • NHibernate.Mapping.Attributes-2.0

image

3. Create a new folder in the in the Castle.Facilities.NHibernateIntegration project. The project structure should look like the following:

image

4. In the previously created folder add the following:

  • An interface called INhibernateMapper This is the base for all the NHibernate mapper types.
  • A class named DefaultNHibernateMapper.
  • A class named AttributeNhibernateMapper.
  • A class named FluentNhibernateMapper.
    The class diagram is shown below:
     image

5. Implement the FluentNHibernateMapper:

 image The RegisterAssemblies method loads the assemblies specified in the <assembly/> section of the castle configuration file and registers the FluentNhibernate mapping classes with NHibernate.

 

6. Expand the Castle.Facilities.NHibernateIntegration project, look for the NHibernateFacility.cs file and locate the protected void RegisterAssemblies(Configuration cfg, IConfiguration facilityConfig) method.

image

Replace the method’s contents with the following code:

image

The method tests if a mapper is declared in the <assembly/> section of the castle configuration file. If yes, it will use it. If not it will use the DefaultNhibernateMapper (xml).

 

Conclusion:

The schema of the configuration file does not change if the preferred mapping type is xml. If the mappings are defined using FluentNHibernate then the castle configuration file transforms like the following:

image

The extra attribute – mapper  - specifies which IHNibernateMapper implementation to use for loading the NHibernate mappings.

The custom mappers can be implemented in a separate assembly. But the interface (IHNibernateMapper) needs to be located the Castle.Facilities.NHibernateIntegration assembly, because the entry point for loading the NHibernate mapping assemblies is the RegisterAssemblies method in the NHibernateFacility class.

The full source code can be downloaded here.

 
Posted by Voicu Matei on 23-Jun-09
1 Comments  |  Trackback Url | Bookmark this post with:        
 

Comments


wayne douglas commented on Wednesday, 24-Jun-2009
Wow!! thought about submitting this to the project?!

Your name  *
Email  *
Your URL 
Comment  *

Please enter the text from the image


Contact us - Raas Van Gaverestraat 83, 9000 Gent, Belgium - Tel. +32 (9) 330.15.00 - Privacy Statement - Sitemap - Sign In Developed with Microsoft Office SharePoint Server 2007