Using NHibernate and Fluent Mappings.
We have a property (Groupable) of a class (GroupAssignment) which implements an interface (IGroupable). Another property on the same class is an enumeration (ItemType) which specifies where the IGroupable is sourced from, i.e. which table.
Can we map this configuration so that NHibernate populates the IGroupable property, linking to the correct table based on the Enum discriminator? If so, how can we achieve this?
Please note the IGroupable property has a one to one relationship with GroupAssignment class.
public class GroupAssignment
{
public virtual new long ID { get; private set; }
public virtual Group Group { get; set; }
public virtual TypeEnum.TypeGroupItem ItemType { get; set; }
public virtual IGroupable Groupable { get; set; }
}
Ideally NHibernate should get the IGroupable from
TableA when ItemType == TableA,
TableB when ItemType == TableB etc.
I should also add that each of the tables directed by the ItemType enum are mapped to classes which implement the IGroupable interface.
You can achieve this with
<any>. You don’t even need the enum; NHibernate will take care of setting the correct discriminator based on the concrete Groupable.Check out 5.2.4. Any type mappings