views:

692

answers:

1

I'm using Fluent NHibernate in order to auto map my entities.

This is the code I'm using for the auto mapping:

new AutoPersistenceModel()
  .AddEntityAssembly(Assembly.GetAssembly(typeof(Entity)))
  .Where(type => type.Namespace.Contains("Domain") && type.BaseType != null && type.BaseType.Name.StartsWith("DomainEntity") && type.BaseType.IsGenericType == true)
  .WithSetup(s => s.IsBaseType = (type => type.Name.StartsWith("DomainEntity") && type.IsGenericType == true))
  .ConventionDiscovery.Add(
      ConventionBuilder.Id.Always(x => x.GeneratedBy.Increment())
);

This works just fine. But now I need to have Eager Loading in one single object of my domain. Found this answer. But when I add the line .ForTypesThatDeriveFrom<IEagerLoading>(map => map.Not.LazyLoad()) to the code and run it I get the following exception:

  • Error while trying to build the Mapping Document for IEagerLoading

Notice that I'm using an interface (IEagerLoading) to mark the objects that I want eager load.

Can anyone help how to do this? Remember that I want to keep the auto mapping functionality.

Thanks

+2  A: 

The problem you're hitting is that ForTypesThatDeriveFrom<T> is a bit misleadingly named, and that it really means ForMappingsOf<T>, so it's trying to find a ClassMap<IEagerLoading> which obviously doesn't exist.

I believe you should be able to handle this with a custom IClassConvention. This is off the top of my head, but should work:

public class EagerLoadingConvention : IClassConvention
{
  public bool Accept(IClassMap target)
  {
    return GetType().GetInterfaces().Contains(typeof(IEagerLoading));
  }

  public void Apply(IClassMap target)
  {
    target.Not.LazyLoad();
  }
}
James Gregory