I have a very extensible and modular application. I want to extend mapped Entites from other assemblies. Yet, I still need to operate on the base classes.
Example:
Parent class mapping in Assembly A:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Table("Persons");
Id(x => x.Id).GeneratedBy.Assigned();
}
}
Child class mapping in Assembly B:
public class EmployeeMap : SubclassMap<Employee>
{
public EmployeeMap()
{
Table("Persons");
Extends(typeof(Person));
KeyColumn("Id");
LazyLoad();
HasMany<Assignments>(x => x.Assignments).KeyColumn("Id").Inverse().Cascade.AllDeleteOrphan().LazyLoad().NotFound.Ignore();
}
}
Now whenever I create a Person in some code of Assembly A it is saved as Employee by NHibernate. And that leads to a class cast exception due to proxying whenever I save a Person and try to refresh it in Assembly A. Assembly A must not have a dependency on Assembly B.
I need to operate on the parent class in all methods of assembly A. The child class is only used in other assemblies.
How can I map something like that? How can I tell NHibernate to just save it as the parent class? I use SaveOrUpdate to persist the entities; How can I correctly extend entities and yet save them to the same table without discriminator? Can’t NHibernate differentiate by object type? Is there a workaround?
I do not want to specify manual proxies as I’d have to create a proxy for every entity! I cannot use the visitor pattern due to dependency problems.
I need a way to extend a mapped entity in a different assembly without such problems! The database is legacy, I cannot change it. How would you work around the issue?
Solved it by using a HasOne Mapping on the same Table and not using a subclass. This does not produce ideal code but is reather free of problems.