0

Currently, I am using the following code to ignore base types with Fluent NHibernate automapping:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .IgnoreBase<MyCore.BaseTypes.AmazingBaseType>()
            .IgnoreBase<MyCore.BaseTypes.AnotherAmazingBaseType>()
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

Is there a way to ignore base types by a namespace (i.e. MyCore.BaseTypes) instead of having to use the IgnoreBase() method for every base type I have?

I tried accomplishing this using my overriden ShouldMap(Type) method in DefaultAutomappingConfiguration-extended class (i.e. MyDefaultAutomappingConfiguration) to accomplish this but it still tried to map the base types:

public class MyDefaultAutomappingConfiguration: DefaultAutomappingConfiguration {
    public override bool ShouldMap(Type type) {
        return !IsBaseType(type);
    }

    private bool IsBaseType(Type type) {
        return type.Namespace == typeof(MyCore.BaseTypes).Namespace;
    }
}
gabe
  • 1,873
  • 2
  • 20
  • 36

3 Answers3

1

What does your MyAutomappingConfiguration look like? You can override ShouldMap(Type) with something like:

public override bool ShouldMap(System.Type type)
{
  return 
    type.BaseType.Namespace == typeof (MyCore.BaseTypes.BaseIWant).Namespace;
}
David R. Longnecker
  • 2,897
  • 4
  • 29
  • 28
1

If your base types are abstract they wont be included in the mapping.

Phill
  • 18,398
  • 7
  • 62
  • 102
  • it seems like it's still trying to map them for some reason because I see them showing up in my conventions... – gabe Nov 22 '10 at 21:13
  • Thats strange. Fluent wiki basically says it shouldn't be included unless you specifically tell it to be included. http://wiki.fluentnhibernate.org/Auto_mapping#Abstract_base-classes – Phill Nov 23 '10 at 02:38
1

You can do it directly in your configuration like this:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .Where(t => t.Namespace != "MyCore.BaseTypes")
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
Variant
  • 17,279
  • 4
  • 40
  • 65
  • so is this the only way to do this? – gabe Nov 22 '10 at 21:13
  • I don't know if it's the only way but it's the most common and straight forward method. That's what I use anyway :) – Variant Nov 22 '10 at 21:32
  • i'm marking this is as the answer. i couldn't get the change to the ShouldMap() method my AutomappingConfiguration to work. – gabe Nov 22 '10 at 22:13
  • actually, i marked this as the answer too hastily... i get a runtime error that tells me you can't use the Where method in conjunction with AutomappingConfiguration... – gabe Nov 24 '10 at 19:47
  • well, the Where was the way to do it up until recently. I used it in several projects in the past. It seems like ShouldMap replaces it as the way to go in the recent FNH release. – Variant Nov 24 '10 at 21:54
  • Here's a post from James Gregory announcing the change: http://fluentnhibernate.org/blog/2010/05/23/feature-focus-automapping.html – Variant Nov 24 '10 at 21:55