So the problem with .UseOverridesFromAssemblyOf is that they do not duplicate type.
Code snipped from FluentNHibernate:
public AutoPersistenceModel UseOverridesFromAssembly(Assembly assembly)
{
this.alterations.Add(new AutoMappingOverrideAlteration(assembly));
return this;
}
public AutoMappingAlterationCollection Add(IAutoMappingAlteration alteration)
{
if (!this.alterations.Exists(a => a.GetType() == alteration.GetType()))
{
this.alterations.Add(alteration);
}
return this;
}
You can see that adding duplicate type is not allowed.
After investigation FNH code I found that they finally alter the AutoMappingOverrideAlteration and I just write an extension to apply this altering
public static class AutoPersistenceModelExtension
{
public static AutoPersistenceModel AddMappingAssembly(this AutoPersistenceModel model, Assembly assembly)
{
new AutoMappingOverrideAlteration(assembly).Alter(model);
return model;
}
public static AutoPersistenceModel AddMappingFromAssemblyOf<T>(this AutoPersistenceModel model)
{
return model.AddEntityAssembly(typeof (T).Assembly);
}
}
The result is:
Our application is build based on components, each components provide own Entities and Mappings for those entities. Each component should be derived from a BaseComponent class which has an abstract method to Alter the model.
Ex of FulfillmentModuleRegistration:
public override void AddToModel(AutoPersistenceModel autoPersistenceModel)
{
autoPersistenceModel.AddEntityAssembly(typeof(SupplierListItem).Assembly);
autoPersistenceModel.AddMappingFromAssemblyOf<SupplierListItemMappingOverride>();
}
and our global.asax.cs:
var autoPersistenceModel = new AutoPersistenceModelGenerator().Generate();
//TODO: Refactor this, instead of instantiate each module we have to go thought a list of loaded module and apply their model alteration
new QuestionBankModuleRegistration().AddToModel(autoPersistenceModel);
new FulfilmentServiceAreaRegistration().AddToModel(autoPersistenceModel);
var cfg = NHibernateSession.Init(
this.webSessionStorage,
new[] { this.Server.MapPath("~/bin/baseMappings.dll") },
autoPersistenceModel,
this.Server.MapPath("~/Hibernate.cfg.xml"));
baseMappings.dll contains our base entities like : Users, Lookups, etc. So it could be any assembly which contains base, default mappings, conventions, etc.