Monday, April 6, 2009

JaNeeType

In our current project we use NHibernate for the Data Access Layer. For mapping a boolean to Y/N char(1) database column, NHibernate provides the YesNoType type. Unfortunately this doesn't work for our database, which contains J (from the Dutch 'Ja') for true.

The solution is simple. Introduce a new user type JaNeeType, which derives from CharBooleanType.
    class JaNeeType : CharBooleanType
{
protected override string FalseString
{
get
{
return 'N';
}
}

protected override string TrueString
{
get
{
return 'J';
}
}
}


At least, that is what I thought. It turns out that the constructor of CharBooleanType is internal, an oversight in NHibernate 1.2. Now the advantage of using Open Source tools comes into sight. I was able to work around this issue by copying the necessary source from CharBooleanType and its base types, until I could compile the class. The end result can be downloaded.