Working on a legacy application at the moment and we are replacing the data access with NHibernate, since the old data access didn’t do eager/lazy loading, parts of the system were doing 2000+ queries to create relationships…
Everything is fine except I have absolutely no idea how to map one scenario.
We have a ‘Shift’ and a ‘MultiResourceShift’
public class Shift
{
public int Id { get; protected set; }
public string EmployeeName { get; set; }
public IEnumerable Breaks { get; set; }
...
}
Multi Resource Shift on the otherhand, is effectively a Shift, that can have multiple employees assigned to it.
public MultiResourceShift : Shift
{
public IEnumerable Employees { get; set; }
public bool IsMultiResource { get; set; }
}
The database structure is mapped as a one-to-many like:
+--------------------+ | Shift | +--------------------+ | ShiftId | | EmployeeName | | IsMultiResource | | ... | +--------------------+ +--------------------+ | MultiResourceShift | +--------------------+ | MultiResourceId | | ShiftId | | EmployeeId | | ... | +--------------------+
Ideally this needs to be queried where the result returns a collection of Shifts, or MultiResourceShifts.
At the moment this is achieved by iterating over the reader, and if it’s a Shift it’s Mapped and added to the collection, if it’s a Multi Resource, an instance of MultiResourceShift is created and populdated from the shift data, and the Employees are loaded, and it’s then added to the collection.
Anyone know if this is possible, how I would map it and query it.
http://nhforge.org/blogs/nhibernate/archive/2011/02/16/get-load-polymorphism-in-nhibernate-3.aspx
This blog post on the NH blog is exactly what I was after and solved the problem. We changed our model slightly to create the discriminator.