0

I use FluentNHibernate with AutoMapping to map my persistent classes. The default Table per Sub-Class mapping works fine for almost all of my class hierarchies, except one: Here I have an abstract base class “A”, A has all the data fields needed. The Subclasses “B” , “C”, … “X” differ only in behavior. “Table per Class” would lead to a lot of unwanted tables.
I want to create an Override Class to create a single table A ( I can do this with an IncludeBaseClass Override. But how do I set up the Discriminator Override class which places all the sub-classes in this table as well? The fluent documentation suggests the following:

public override bool IsDiscriminated(Type type)
{
    return type.In(typeof(ClassOne), typeof(ClassTwo));
}

I thinik this would boildown to this for my case:

public override bool IsDiscriminated(Type type)
{
    return (type == typeof(A));
}

But what would be the Override class to place this method in?

k.c.
  • 1,755
  • 1
  • 29
  • 53

2 Answers2

1

place this method in a class which inherits from DefaultAutomappingConfiguration.
also, might need to do: return (type == typeof(A) || type.IsSubclassOf(typeof(A));

Vadim
  • 17,897
  • 4
  • 38
  • 62
J. Ed
  • 6,692
  • 4
  • 39
  • 55
  • How do i make sure the class is used during (auto)mapping? – k.c. Jun 20 '11 at 14:42
  • I have created this sample class: `public class TablePerHierarchyOverride : FluentNHibernate.Automapping.DefaultAutomappingConfiguration { public override bool IsDiscriminated(Type type) { return (type == typeof(Code)||type == typeof(GenderCode)); } }` – k.c. Jun 20 '11 at 14:50
  • You are absolutely right. A breakpoint will show whether or not code is called. And the method in my class is not. – k.c. Jun 21 '11 at 07:04
  • in that case- make sure that you've defined `Conventions.AddFromAssemblyOf` or `Conventions.Add` in your configuration, that your class is public (not internal) and is in the same assembly and namespace as was defined by `AddFromAssemblyOf` – J. Ed Jun 21 '11 at 09:28
0

The “IsDiscriminated” method is part of the “DefaultAutomappingConfiguration” class. By overriding this class you can alter the way classes are mapped:

public class MyAutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        return type.Namespace != null &&
               type.Namespace.Contains("Models");
    }

    public override bool IsDiscriminated(Type type)
    {
        return type == typeof(Code);
    }
}

Note: The ShouldMap is overriden aswell as the use of this configuration class prevents the usage of the “Where” clause in the mapping It is passed to the mapping process like so:

  AutoMap.Assemblies(new MyAutoMappingConfig(), assembliesToMap.ToArray()).                 Conventions.AddFromAssemblyOf<BaseEntity>();
k.c.
  • 1,755
  • 1
  • 29
  • 53