views:

289

answers:

2

I have a class

public class Site {
  public DateTime SiteMonth {get; set;}
}

in the database the SiteMonth is represented as an integer in yyyymm format.

Is there a way to map this in NHibernate without introducing a new property on my Site class?

+2  A: 

Yes - make a simple IUserType that maps between your integer format and a DateTime. Then set the type attribute on the property element to the AssemblyQualifiedName of that user type.

Ayende has an example on how to implement a user type.

Oh yeah, and if you are using Fluent NHibernate to do your mappings, you can do it like this:

Map(d => d.MyFunkyWeirdLegacyDateTime)
    .SetAttribute("type", typeof(MyCustomDateTime).AssemblyQualifiedName);
mookid8000
`CustomTypeIs<MyCustomDateTime>()` is the preferred method, instead of `SetAttribute`.
James Gregory
Ah, thanks for pointing that out! Fluent NHibernate is smashing, by the way :-)
mookid8000
+1  A: 

I was just in the Fluent NHibernate Wiki and came across this, which is in the AutoMapping conventions, but in the Fluent Mapping Conventions section it mentioned that the AutoMapping Conventions will work for Fluent Mapping as well.

RKitson