How to map a char to bool?

1,870 views
Skip to first unread message

Influently NHiberater

unread,
Sep 14, 2011, 11:37:02 AM9/14/11
to fluent-n...@googlegroups.com
In my MySql database table, one column "is_active" is defined as char and it only contains values '0' or '1'.  So in my business model, I'd like to declare a property IsActive as a bool.
 
But then how do I do the mapping?
 
I tried Map(x=>x.IsActive).Column("is_active"), but this won't work since column ("is_active") is char and x.IsActive is bool. 
 
Any hint?  Thank you very much.  I've been googling this for days, but can't find much helpful info. Thanks a lot!

Tim Scott

unread,
Sep 14, 2011, 1:55:39 PM9/14/11
to fluent-n...@googlegroups.com
Try this:

Map(x=>x.IsActive).Column("is_active").CustomType<CharBooleanType>()

--
Tim Scott
Lunaverse Software

> --
> You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/l1Aj-Uz8RjkJ.
> To post to this group, send email to fluent-n...@googlegroups.com (mailto:fluent-n...@googlegroups.com).
> To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com (mailto:fluent-nhibern...@googlegroups.com).
> For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


Influently NHiberater

unread,
Sep 14, 2011, 2:26:13 PM9/14/11
to fluent-n...@googlegroups.com

On Wednesday, September 14, 2011 1:55:39 PM UTC-4, Tim Scott wrote:
Try this:

Map(x=>x.IsActive).Column("is_active").CustomType<CharBooleanType>()

--
Tim Scott
Lunaverse Software

Are you indicating that I have to implement a custom type called CharBooleanType?  Thanks.

Influently NHiberater

unread,
Sep 14, 2011, 2:43:18 PM9/14/11
to fluent-n...@googlegroups.com

On Wednesday, September 14, 2011 2:26:13 PM UTC-4, Influently NHiberater wrote:

On Wednesday, September 14, 2011 1:55:39 PM UTC-4, Tim Scott wrote:
Try this:

Map(x=>x.IsActive).Column("is_active").CustomType<CharBooleanType>()

--
Tim Scott
Lunaverse Software

 
OK, just found that  CharBooleanType is under NHibernate.Type.  So, I tried like what you said:
 
Map(x => x.IsActive).Column("is_active").CustomType<NHibernate.Type.CharBooleanType>();
 
No build error, but at compile time, I got this:
 
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  ----> FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  ----> NHibernate.MappingException : Could not compile the mapping document: (XmlDocument)
  ----> NHibernate.MappingException : Could not instantiate IType CharBooleanType: System.MissingMethodException: Cannot create an abstract class.
 
 
 
 

Tim Scott

unread,
Sep 14, 2011, 3:44:07 PM9/14/11
to fluent-n...@googlegroups.com
I didn't notice that CharBooleanType is abstract. There's TrueFalseType and YesNo type, neither of which are what you need. Two ideas:

One of the answers to this post says you can do it with query substitutions alone. You might try that. You probably want to set query substitutions in any case.
http://stackoverflow.com/questions/7404991/fluentnhibernate-how-to-map-database-char-to-c-bool

Otherwise, you can do it for sure with a custom IUserType:
http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

Copy the sample code and just change the NullSafeGet and NullSafeSet to the behavior you want. Then map your custom type in FNH (instead of CharBooleanType).

> --
> You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/f9yCrRQed_YJ.

James Gregory

unread,
Sep 15, 2011, 7:25:11 AM9/15/11
to fluent-n...@googlegroups.com
I'm guessing you could probably create your own subclass of CharBooleanType and just override the TrueString and FalseString properties to be what you need them to be.

However, I think this is exactly what query substitutions was designed for. Certainly most places I've seen have had 1 and 0 specified in the substitutions.

Both should work though.

Tim Scott

unread,
Sep 15, 2011, 9:04:07 AM9/15/11
to fluent-n...@googlegroups.com
You can't derive from CharBooleanType. The c-tor is internal. That's what they create IUserType for. It's more work but limits break-ability of changes.

> --
> You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/sHWjC5uxiNwJ.

Influently NHiberater

unread,
Sep 15, 2011, 10:26:25 AM9/15/11
to fluent-n...@googlegroups.com


On Wednesday, September 14, 2011 3:44:07 PM UTC-4, Tim Scott wrote:
I didn't notice that CharBooleanType is abstract. There's TrueFalseType and YesNo type, neither of which are what you need. Two ideas:

One of the answers to this post says you can do it with query substitutions alone. You might try that. You probably want to set query substitutions in any case.
http://stackoverflow.com/questions/7404991/fluentnhibernate-how-to-map-database-char-to-c-bool

But there is no XML configuration for FluentNHibernate, so I don't understand how query subsititution works.
 

Otherwise, you can do it for sure with a custom IUserType:
http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

Copy the sample code and just change the NullSafeGet and NullSafeSet to the behavior you want. Then map your custom type in FNH (instead of CharBooleanType).

After some research, I kinda realized that I may have to implement my own user type, but then thought if this is an overkill for such a small task (converting a char/string to bool).  This kind of casting comes up pretty often, maybe this should be built into FluentNHibernate?  But for now, if this is something I have to do, I'll do it. Not the next time. :-)

Michael Möhle

unread,
Sep 15, 2011, 10:47:05 AM9/15/11
to fluent-n...@googlegroups.com

Maybe you can use this baseclass this time AND the net time ;-)

 

http://darrell.mozingo.net/2009/02/10/generic-nhibernate-user-type-base-class/

To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/gdfLrB2oWoQJ.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.

Influently NHiberater

unread,
Sep 15, 2011, 2:27:59 PM9/15/11
to fluent-n...@googlegroups.com


On Thursday, September 15, 2011 10:47:05 AM UTC-4, mbd-team wrote:

Maybe you can use this baseclass this time AND the net time ;-)

 

http://darrell.mozingo.net/2009/02/10/generic-nhibernate-user-type-base-class/


I followed your example, but could not make it work.  I don't seem to have the To method like you have in MoneyUserType:

var amount = NHibernateUtil.Decimal.NullSafeGet(rs, names[0]).To<decimal?>()

Also, do you have an example showing how you consume your custom type? How come I don't have this To<T> method like you do?

Influently NHiberater

unread,
Sep 15, 2011, 2:59:31 PM9/15/11
to fluent-n...@googlegroups.com


On Wednesday, September 14, 2011 3:44:07 PM UTC-4, Tim Scott wrote:
I didn't notice that CharBooleanType is abstract. There's TrueFalseType and YesNo type, neither of which are what you need. Two ideas:

One of the answers to this post says you can do it with query substitutions alone. You might try that. You probably want to set query substitutions in any case.
http://stackoverflow.com/questions/7404991/fluentnhibernate-how-to-map-database-char-to-c-bool

Otherwise, you can do it for sure with a custom IUserType:
http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

Copy the sample code and just change the NullSafeGet and NullSafeSet to the behavior you want. Then map your custom type in FNH (instead of CharBooleanType).

I followed the example at lostechies.com, and this is what I have for NullSafeGet and NullSafeSet:

  public object NullSafeGet(Idr dr, string[] names, object owner) {
            var obj = NHibernateUtil.String.NullSafeGet(dr, names[0]);
            if (String.IsNullOrWhiteSpace(Convert.ToString(obj))) {
                return null;
            }

            if (Convert.ToString(obj) != "1" && Convert.ToString(obj) != "0") {
                throw new Exception("Expected data to be '1' or '0', but was " + obj);
            }

            return Convert.ToString(obj) == "1";
        }

        public void NullSafeSet(IDbCommand dbCommand, object value, int index) {
            if (value == null) {
                ((IDataParameter)dbCommand.Parameters[index]).Value = DBNull.Value;
            }
            else {
                var yes = (bool)value;
                ((IDataParameter)dbCommand.Parameters[index]).Value = yes ? "1" : "0";
            }
        }
 
Then in my Mappings.cs, I have

Map(x => x.IsInactive).Column("inactive").CustomType<CustomBooleanType>();

No build error, but at run time, I get this:

 System.InvalidCastException : Unable to cast object of type 'System.Boolean' to type 'MyProject.Mappings.CustomBooleanType'.

So, what am I missing? Thanks.

Tim Scott

unread,
Sep 15, 2011, 3:06:28 PM9/15/11
to fluent-n...@googlegroups.com
Sounds like the property is not being mapped to the custom type. To check that you can output your hbm (XML) mapping files. If you don't know how to do that Google will tell you.

On Thursday, September 15, 2011 at 1:59 PM, Influently NHiberater wrote:

>
>
> On Wednesday, September 14, 2011 3:44:07 PM UTC-4, Tim Scott wrote:
> > I didn't notice that CharBooleanType is abstract. There's TrueFalseType and YesNo type, neither of which are what you need. Two ideas:
> > One of the answers to this post says you can do it with query substitutions alone. You might try that. You probably want to set query substitutions in any case.
> > http://stackoverflow.com/questions/7404991/fluentnhibernate-how-to-map-database-char-to-c-bool
> > Otherwise, you can do it for sure with a custom IUserType:
> > http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/
> > Copy the sample code and just change the NullSafeGet and NullSafeSet to the behavior you want. Then map your custom type in FNH (instead of CharBooleanType).
>

> I followed the example at lostechies.com (http://lostechies.com), and this is what I have for NullSafeGet and NullSafeSet:

> To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/q9Nwc_PAsEAJ.

Reply all
Reply to author
Forward
0 new messages