Using NHibernate ICompositeUserType with a value type

后端 未结 3 2024
名媛妹妹
名媛妹妹 2021-01-12 00:15

I have a domain model object which has properties of type System.DateTimeOffset. I\'m using a database which doesn\'t support this type natively, so I\'m planning to store i

3条回答
  •  不知归路
    2021-01-12 00:55

    public void SetPropertyValue(object component, int property, object value)
    

    I have code that implements DateTime from two int fields.

    The code essentially is a switch on property (0 being date, 1 being time, in my case) and at the end of each case statement the component object is reassigned a new DateTime instance.

    property = 0 (Date):

    // code to calculate year, month, day from object value
    DateTime dt = (DateTime)component;
    dt = new DateTime(year, month, day, dt.Hour, dt.Minute, dt.Second);
    

    property = 1 (Time):

    // code to calculate hours, minutes, seconds from object value
    DateTime dt = (DateTime)component;
    dt = new DateTime(dt.Year, dt.Month, dt.Day, hours, minutes, seconds);
    

    No idea if this is good / bad, but it works for me (aka I have no problem changing what component points to, ref or not).

提交回复
热议问题