I'm testing out NHibernate to be the solution to my company's ORM needs. To do this, I've produced a small test model based on a school, providing some useful edge-cases for NHibernate to handle.
I'm having problems finding out how to map a custom structure as a component of an entity without using theIUserType
interface. I should stress that it is an important requirement that the domain classes are in a separate assembly from our NHibernate code, and that the domain assembly does not have a reference to NHibernate.
The custom structure is Time
, used to represent a time of day in hours and minutes. It's a very simple immutable structure and only provided to illustrate the problem of a custom structure. The constructor takes a single argument which is hours and minutes, as an integer in the form hhmm
.
public struct Time
{
public Time(int hoursAndMinutes)
{
// Initialize Structure //
}
public int Hours { get; private set; }
public int Minutes { get; private set; }
public int HoursAndMinutes { get; private set; }
}
This structure is used as a component of the Lesson
class to store the time of day the lesson starts:
public class Lesson
{
public int ID { get; private set; }
public Teacher Teacher { get; internal set; }
public DayOfWeek Day { get; set; }
public Time StartTime { get; set; } // <-- Custom Type
public int Periods { get; set; }
}
This class maps directly to this table:
CREATE TABLE Lessons
(
ID INT,
Subject NVARCHAR(128)
TeacherID INT,
Day VARCHAR(9),
StartTime INT, // <-- Maps to custom type.
Periods INT
)
I am looking for a way to map this structure as a component of the Lesson
class, so that NHibernate will read a property value on the structure (like any other component) to get a value for the column, but will initialize a new instance of the structure by passing the column value to the constructor when reading the value from the column into the entity.
If you have any suggestions, that'd be super. If you want to tell me this can't be accomplished without using IUserType
, that's a fine answer too.