In order to load the mappings you’ve written using Fluent NHibernate you need to call the extension method “AddMappingsFromAssembly” on the configuration. The “LocalSessionFactoryObject” defined in Spring.Net supports out of the box the loading of .hbm files from an assembly or a location in the file system. Luckily this class can be extended with ease. The code below is all you need to use Fluent NHibernate with Spring.Net, any suggestions for a better name are welcome.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using FluentNHibernate;
using System.Reflection;
using NHibernate.Cfg;
namespace SessionFactories
{
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
///
/// The mapping assemblies.
public string[] FluentNhibernateMappingAssemblies
{
get;
set;
}
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
if(FluentNhibernateMappingAssemblies != null)
{
foreach(string assemblyName in FluentNhibernateMappingAssemblies)
{
config.AddMappingsFromAssembly(Assembly.Load(assemblyName));
}
}
}
}
}
Update 29 June: please check the comments since a new version brought some changes to this blog post.