The case
OK, so I worked around the first issues with the setup of the NHibernateIntagration facility and fluent NHibernate, and now are new issues just showing up.
So I wrote my first mapping:
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Id(x => x.Number).GeneratedBy.Assigned();
Map(x => x.Name);
}
}
This looks pretty slick – in fact this is looking so darn cool, I want it to work!
But what is showing up in my testrunner is not very re-assuring: No persister found for Entity Account.
What the hell?!
The setup
So what happend so far? After getting my solution ready to compile, the next step is to add the fluent mapping to the current NHibernate configuration. Since the configuration is being created within the integration facility this doesn’t seem trivial. But actually, taking a closer look, this isn’t that complicated:
<facility id="nhibernate"
isWeb="false"
type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"
configurationBuilder="FluentConfigurationBuilder, Core">
The key is the configurationBuilder
. This offers a hook in the configuration-building-process of the facility.
Next step is to implement the configuration-builder:
public class FluentConfigurationBuilder : IConfigurationBuilder
{
public Configuration GetConfiguration(IConfiguration facilityConfiguration)
{
var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
var configuration = defaultConfigurationBuilder.GetConfigutation(facilityConfiguration);
configuration.AddMappingsFromAssembly(Assembly.LoadFrom("Core.dll"));
return configuration;
}
}
This is the recommended route to read the fluent mappings from the assembly and to add them to the NHibernate configuration. The key is the AddMappingsFromAssembly
extension-method.
So, this is where I stand right now – and this is failing.
The solution
Looking at the source of the fluent NHibernate extension-method AddMappingsFromAssembly
reveals some truth:
public static Configuration AddMappingsFromAssembly(this Configuration configuration, Assembly assembly)
{
var models = new PersistenceModel();
//models.AddMappingsFromAssembly(assembly);
models.Configure(configuration);
return configuration;
}
So there it is – black-on-white: the actual mapping is commented out – and no-one knows really why.
Ok, so my current (and working) code looks like this:
public class FluentConfigurationBuilder : IConfigurationBuilder
{
public Configuration GetConfiguration(IConfiguration facilityConfiguration)
{
var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
var configuration = defaultConfigurationBuilder.GetConfigutation(facilityConfiguration);
var model = new PersistenceModel();
model.AddMappingsFromAssembly(Assembly.LoadFrom("Core.dll"));
model.Configure(configuration);
return configuration;
}
}