Reputation: 1835
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?
Upvotes: 0
Views: 989
Reputation: 1835
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>();
Upvotes: 0
Reputation: 6752
place this method in a class which inherits from DefaultAutomappingConfiguration
.
also, might need to do: return (type == typeof(A) || type.IsSubclassOf(typeof(A));
Upvotes: 1