Question

What is the best way to get the reverse behavior of the YesNo Boolean type in NHibernate mapping?

I want 'Y' to mean false and 'N' to mean true.

Is there a NoYes type? Do you write a custom type? something really easy?

This issue of needing to reverse the Boolean exists on at least one field on over 40 tables. Trying to adapt to a legacy database.

Was it helpful?

Solution

I have found that dealing with weird data formats in legacy databases can be achieved easily by implementing custom types. For example, I recently made a simple user type to map DateTime to 8 digit numbers on the form yyyyMMdd, which happened to be the way the dates were stored in a DB2 dump I had to use.

Los Techies have an example on implementing IUserType that you can use to solve your problem: Mapping Strings to Booleans Using NHibernate's IUserType.

OTHER TIPS

I had a similar problem. The easier solution that I found was extend the CharBooelanType from NHibernate and use the new type on the mapping. It would be like that:

using NHibernate.Type;
using NHibernate.SqlTypes;

namespace MyAssembly.MyNamespace 
{
    public class NewBoolType : CharBooleanType 
    {
        public NewBoolType()
            : base(new AnsiStringFixedLengthSqlType(1)) { }

        protected override string TrueString 
        {
            get { return "N"; }
        }

        protected override string FalseString 
        {
            get { return "Y"; }
        }

        public override string Name 
        {
            get { return "inverted_bool"; }
        }
    }
}

And the mapping would be like that:

<property name="MyProperty" column="MyColumn" type="MyAssembly.MyNamespace.NewBoolType, MyAssembly" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top