Mapping Strings to Booleans Using NHibernate’s IUserType
Update: I failed to realized that the functionality of converting “Y” or “N” to a boolean is already built into NHibernate by doing type=”YesNo” (see comments). I’ll leave this post up just for the academics of creating a IUserType. Go figure that I would come up with something that’s built in!
We have some legacy data where boolean values are stored as strings. “Y” is true and “N” is no. We’re also creating entities which have properties that map to this data. It would be just plain evil to have to create all the boolean properties as strings! We previously solved this problem by creating our own .NET type that had a string property for the “Y” or “N” value. This type had an implicit cast defined as a bool, so we could treat it as a boolean in expressions and assignments. We mapped this in NHibernate as a component with the single string property and this worked pretty well, but I suspected that there was a better way. Today I bought the e-book version of NHibernate in Action and quickly read through the section on creating an implementation of IUserType. I then came up with the following class that allows me to keep my types as true booleans in the code.
public class YesNoType : IUserType { public bool IsMutable { get { return false; } } public Type ReturnedType { get { return typeof(YesNoType); } } public SqlType[] SqlTypes { get { return new[]{NHibernateUtil.String.SqlType}; } } public object NullSafeGet(IDataReader rs, string[] names, object owner) { var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]); if(obj == null ) return null; var yesNo = (string) obj; if( yesNo != "Y" && yesNo != "N" ) throw new YourException("Expected data to be 'Y' or 'N' but was '{0}'.", yesNo); return yesNo == "Y"; } public void NullSafeSet(IDbCommand cmd, object value, int index) { if(value == null) { ((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value; } else { var yes = (bool) value; ((IDataParameter)cmd.Parameters[index]).Value = yes ? "Y" : "N"; } } public object DeepCopy(object value) { return value; } public object Replace(object original, object target, object owner) { return original; } public object Assemble(object cached, object owner) { return cached; } public object Disassemble(object value) { return value; } public new bool Equals(object x, object y) { if( ReferenceEquals(x,y) ) return true; if( x == null || y == null ) return false; return x.Equals(y); } public int GetHashCode(object x) { return x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode(); } }
The mapping looks something like:
<property name="Active" type="NHTypes.YesNoType, NHTypes" column="ACTIVE_YN" not-null="false" />
I’m not going to go into detail and explain what all the parts are and do (see link), but I thought that I would share the class. Please let me know if you spot any problems.