Imagine we have this entities:
public interface ISomeone
{
}
public abstract class Parent
{
}
public class Child : Parent , ISomeone
{
}
public class Orphan : ISomeone
{
}
public class MyHeart
{
public ISomeone Person {get;set;}
}
then how is possible to mapping theses all classes in nhibernate? I prefer to use separate tables for "Parent", "Orphane" and "MyHeart" class. "Parent" , "Orphan" and "MyHeart" should be persist as aggrigate root and MyHeart could has a relation to any entity that implemented "ISomeone" interface. so it could expande across diffrent entity types with diffrent id form diffrent tables
Finally I've found the solution the "Any" method is the answer, briefly with this method we define some meta value for each possible value for IPerson in another world every class that implements this interface should be define in "Any" method then nHibernate persists the Id and the MetaValue.
The mapping by code for my example would be like this:
public class MyHeartMapping : ClassMapping<MyHeart>
{
public MyHeartMapping()
{
Id(x => x.Id, x => x.Generator(Generators.Native));
Any(p => p.Person, typeof(long), m => {
m.MetaValue("Child", typeof(Child));
m.MetaValue("Orphane", typeof(Orphane));
m.Columns(i => i.Name("PersonId"), c => c.Name("ClassName")); });
}
}
public class ParentMapping : ClassMapping<Parent>
{
public ParentMapping()
{
Id(x => x.Id, x => x.Generator(Generators.Native));
Discriminator(c => c.Column("Discriminator"));
}
}
public class ChildMapping : SubclassMapping<Child>
{
public ChildMapping()
{
DiscriminatorValue("Child");
}
}
public class OrphaneMapping : ClassMapping<Orphane>
{
public OrphaneMapping()
{
Id(x => x.Id, x => x.Generator(Generators.Native));
}
}