NHibernate IUserType for fake database booleans

In the db schema I’m currently working on, boolean values are represented by columns whose type is tinyint nullable (it’s SQL Server 2005). If such a value equals 1, then it is supposed to be True, 0 means False and if it is null then it is False, too.
This time I would like to show how to use NHibernate IUserType interface to enable automatic conversion from a database field to System.Boolean value
NHibernate.UserTypes.IUserType is a very useful interface when you are to handle some strange db schemas in your data access layer. As you can guess, the thing that we need is a class that implements this interface. You can see it below. The most important methods are NullSafeGet(…) and NullSafeSet(…) as they are responsible for conversion ‘logic’.

namespace MyNamespace
{
  public class ByteAsBool : IUserType
  {
      #region IUserType Members

      public object Assemble(object cached, object owner)
      {
          return cached;
      }

      public object DeepCopy(object value)
      {
          return value;
      }

      public object Disassemble(object value)
      {
          return value;
      }

      public int GetHashCode(object x)
      {
          if (x == null)
              return 0;
          return x.GetHashCode();
      }
      public bool IsMutable
      {
         get { return false; }
      }

      // represents conversion on load-from-db operations:
      public object NullSafeGet(System.Data.IDataReader rs, 
             string[] names, object owner)
      {
          var obj = NHibernateUtil.String.
                 NullSafeGet(rs, names[0]);
          if (obj == null)
              return false;
           byte b = 0;
          try
          {
              if (obj is string)
                  b = byte.Parse(obj as string);
              else
                  b = (byte)obj;
          }
          catch (Exception)
          {
              return false;
          }
          return b == 1;
      }

      // represents conversion on save-to-db operations:
      public void NullSafeSet(System.Data.IDbCommand cmd, 
             object value, int index)
      {
          if (value == null)
          {
              ((IDataParameter)cmd.Parameters[index]).Value = 
                    DBNull.Value;
          }
          else
          {
              var boolValue = (bool)value;
              ((IDataParameter)cmd.Parameters[index]).Value = 
                    boolValue ? (byte)1 : (byte)0;
          }
      }
      public object Replace(object original, object target, 
             object owner)
      {
          return original;
      }

      public Type ReturnedType
      {
          get { return typeof(bool); }
      }

      public NHibernate.SqlTypes.SqlType[] SqlTypes
      {
          get { return new[] { SqlTypeFactory.Byte }; }
      }
      #endregion

      bool IUserType.Equals(object x, object y)
      {
          return object.Equals(x, y);
      }
  }
}

The class ByteAsBool can be now used in NHibernate mappings in the following way:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="SomeAssembly" namespace="MyNamespace.Types">
  <class name="SomeClass" table="Some_Table" >
  <!-- ... -->
    <property name="active" 
        column="ACTIVE" 
        type="MyNamespace.ByteAsBool, SomeAssembly" access="field" />
  <!-- ... -->
  </class>
</hibernate-mapping>

The mapped class is very simple and looks very nice, as there are no ugly bool? types, just simple System.Boolean:

  public class SomeClass : BaseEntity
  {
     protected bool active;
     // ...
  }

SQL Server 2005 issue.

A few days ago I was forced to install and use SQL Server database on my system. A few days later I had to install MS VS .NET 2008. After that, it turned out that the standard utility SQL Server Configuration Manager does not seem to work correctly. It shows a message box that contains the following error:
Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 servers with SQL Server Configuration Manager.
Nie można znaleźć określonego modułu. [0x8007007e]

Unfortunately, I could not find any working solution of this problem (perhaps it is so because my knowledge of Microsoft’s database is less than miserable ) Some pages suggested running Mof-compiler program with *.mof file from SQL Server installation directory but it does not work in my case.

EDIT: After some time I figured out what should done to solve this problem. It is simple. It just requires copying framedyn.dll file from folder: “C:\WINDOWS\system32\wbem” to folder: “C:\WINDOWS\system32”.