Monday, March 7, 2011

NHibernate: Best way to map to DateTime a value stored in a non-standard way

I am just getting started with NHibernate (for the 15th time it would seem) and am having the following problem.

The following table:

Table Facility
Column FACILITY_ID integer
Column NAME varchar2(50)
Column MONTH varchar2(5)

For whatever reason, month is a string instead of a native Date type and looks like this:

"200811" represents 11/01/2008
"200307" represents 07/01/2003
you get the idea

I would like to map it to the following class

public class Facility {
  int Id {get; set;}
  string Name {get; set;}
  DateTime Month {get; set;}
}

I would like to map the MONTH column to the Month property but don't quite know how to approach the situation. Obviously, I could have a protected property string MonthString and have the Month property Parse that column, but that seems icky. Is there a better solution?

From stackoverflow
  • I think the best solution may be to use a custom value type.

  • If you cannot modify your database schema, the answer is IUserType. Here is a good write-up.

    There are many extension points that can help you solve complex problems in NHibernate. The most useful extension point is IUserType.

0 comments:

Post a Comment