Good afternoon,
We have a set of tables where we store boolean values as a nvarchar(1) where '1' = true and '0' = false.
I am trying to implement a custom type for these columns using the following:
Code:
using NHibernate.Type;
using System;
namespace com.purefacts.db.hibernate.type
{
/// <summary>
/// NHibernate transform for the Boolean type.
/// </summary>
///
/// <remarks>
/// We stored in a nvarchar(1) field the values of '1' for TRUE and '0' for FALSE
/// </remarks>
///
public class BooleanType : EnumStringType
{
public BooleanType() : base(typeof(BooleanEnum)) {
}
public override object GetValue(object code) {
if (null == code) {
return "0";
}
switch ((bool)code) {
case true:
return "1";
case false:
return "0";
default:
throw new ArgumentException("Invalid BooleanEnum.");
}
}
public override object GetInstance(object code) {
if (code == null) {
return false;
}
else {
String strCode = ((String)code).ToUpper();
switch (strCode) {
case " ":
return false;
break;
case "1":
return true;
break;
case "0":
return false;
break;
default:
throw new ArgumentException("Cannot convert code '" + code + "' to BooleanEnum.");
}
}
}
}
/// <summary>The BOOLEAN enumeration.</summary>
public enum BooleanEnum
{
TRUE,
FALSE
}
}
and my HBM entry is
Code:
<class...
...
<property name="Active" column="Active" type="com.purefacts.db.hibernate.type.BooleanType, SpringAir.Core" access="property"/>
...
</class>
which seems to be working but looks a little strange, is this the correct way to accomplish this?
Furthermore, why can't I provide my own type values to the TrueFalse type so I can store '1' or '0' instead of 'T' and 'F'
Thanks in advance.