Here's the final approach that I took to organize my overrides:
1) Add an override interface to your application as follows:
using FluentNHibernate.AutoMap;
namespace SharpArch.Data.NHibernate.FluentNHibernate
{
/// <summary>
/// Used by <see cref="AutoPersistenceModelExtensions" /> to add
auto mapping overrides
/// to <see cref="AutoPersistenceModel" />
/// </summary>
public interface IAutoPeristenceModelConventionOverride
{
AutoPersistenceModel Override(AutoPersistenceModel model);
}
}
2) Create an extension method, as follows, to look for every class
which implements IAutoPeristenceModelConventionOverride, in a
particular assembly, and apply the override to AutoPersistenceModel:
using FluentNHibernate.AutoMap;
using System.Reflection;
using System;
using SharpArch.Core;
namespace SharpArch.Data.NHibernate.FluentNHibernate
{
/// <summary>
/// Provides a means to override <see cref="AutoPersistenceModel" /
> conventions with classes
/// that implement <see
cref="IAutoPeristenceModelConventionOverride" />.
/// </summary>
public static class AutoPersistenceModelExtensions
{
public static AutoPersistenceModel
MapConventionOverridesFromAssemblyOf<TOverride>(
this AutoPersistenceModel autoPersistenceModel) where
TOverride : IAutoPeristenceModelConventionOverride {
Assembly assemblyToPullConventionOverridesFrom = typeof
(TOverride).Assembly;
foreach (Type type in
assemblyToPullConventionOverridesFrom.GetTypes()) {
if (typeof
(IAutoPeristenceModelConventionOverride).IsAssignableFrom(type)) {
IAutoPeristenceModelConventionOverride instance =
Activator.CreateInstance(type) as
IAutoPeristenceModelConventionOverride;
if (instance != null)
instance.Override(autoPersistenceModel);
}
}
return autoPersistenceModel;
}
}
}
3) Create a class for each entity that requires a convention override;
for example:
using FluentNHibernate.AutoMap;
using Northwind.Core;
using SharpArch.Data.NHibernate.FluentNHibernate;
namespace Northwind.Data.NHibernateMappings
{
public class CustomerMap : IAutoPeristenceModelConventionOverride
{
public AutoPersistenceModel Override(AutoPersistenceModel
model) {
return model.ForTypesThatDeriveFrom<Customer>(map => {
map.SetAttribute("lazy", "false");
});
}
}
}
4) Include a call to the extension method when setting up the
AutoPersistenceModel; e.g.,
AutoPersistenceModel mappings = AutoPersistenceModel
.MapEntitiesFromAssemblyOf<Customer>()
...
.WithConvention(GetConventions)
.MapConventionOverridesFromAssemblyOf<CustomerMap>();
I'll be including this approach in the next release of S#arp
Architecture which leverages Fluent NHibernate. Hope this helps!
Billy McCafferty
On Feb 9, 9:03 am, Steven Harman <
stevehar...@gmail.com> wrote:
> Being one of the folks currently using the AutoPersistenceModel in an
> unconventional (or rather, just unexpected) manner, I suppose I should speak
> up in favor of allowing, and suggesting, that class-specific convention
> overrides be split out into their own auto mappings files. I'm on board
> w/Andy - this approach keeps things more granular, better seperated, and
> easier to grok when you have a large number of mappings.
>
> That said, I also see James concern that it might not be obvious that these
> small one-off maps are actually overriding some other auto mapping, setup
> elsewhere. However, I think some smart naming conventions, namespacing, and
> a little bit of education could go a long way toward eliminating that
> concern. But then, that's just my opinion.
>
> Perhaps talking about the various ways folks are using FNH mappings (both
> auto and manual), and the lessons we're learning, would be a good topic for
> a VAN Meeting?
>
> -steve
>
> //---- 90% of being smart is knowing what you're dumb at ----//
http://stevenharman.net/
> ...
>
> read more »