-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can I create my own CharBooleanType?
PostPosted: Fri May 05, 2006 4:41 pm 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi!

I was trying to create my own CharBooleanType, because I represent my True/False values using S/N (in portuguese, it means "Sim" and "Não").

The code is below:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Type;
using NHibernate.SqlTypes;

namespace MapeamentoOR.Types
{
    public class SimNaoType : CharBooleanType
    {
        internal SimNaoType() : base(new AnsiStringFixedLengthSqlType(1))
      {
      }
      
      protected override sealed string TrueString
      {
         get { return "S"; }
      }
      
      protected override sealed string FalseString
      {
         get { return "N"; }
      }
              
      public override string Name
      {
         get { return "SimNao"; }
      }
    }
}


When I try to build my solution, I get the following message:

Quote:
The type 'NHibernate.Type.CharBooleanType' has no constructors defined


It isn't possible to inherite the CharBooleanType?

Can you help me? :)

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 4:53 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We use this, which also provides support for NULL (we are using .NET 2.0). Just change references of 'Y' to 'S' ...

Code:
using System;
using System.Data;

using NH  = NHibernate;
using NNH = Nullables.NHibernate;

namespace OurCompany.Domain.DataAccess.Providers.NHibernate.UserTypes
{
   /// <summary>
   /// A NHibernate <see cref="NH.Type.IType"/> for a <see cref="System.Nullable&lt;Boolean&gt;"/>
   /// mapped to a CHAR(1) database column with values 'Y', 'N' or NULL.
   /// </summary>
   public class NullableYesNoType : NNH.NullableTypesType
   {
      public NullableYesNoType() : base(new NH.SqlTypes.StringFixedLengthSqlType(1))
      {
      }

      public override object NullValue
      {
         get {return null;}
      }

      public override System.Type ReturnedClass
      {
         get {return typeof(bool?);}
      }

      public override object Get(IDataReader rs, int index)
      {
         object value = rs[index];

         if (value == DBNull.Value)
         {
            return null;
         }
         else
         {
            return (Convert.ToChar(value) == 'Y');
         }
      }

      public override void Set(IDbCommand cmd, object value, int index)
      {
         IDataParameter parameter = (IDataParameter) cmd.Parameters[index];
         bool? nullableValue = (bool?) value;

         if (nullableValue.HasValue)
         {
            parameter.Value = (nullableValue.Value ? 'Y' : 'N');
         }
         else
         {
            parameter.Value = DBNull.Value;
         }
      }

      public override object FromStringValue(string xml)
      {
         if (xml == null || xml.TrimEnd().Length == 0)
         {
            return null;
         }
         else
         {
            return (xml[0] == 'Y');
         }
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 9:47 am 
Senior
Senior

Joined: Fri Jan 13, 2006 2:50 pm
Posts: 123
Location: Blumenau / SC / Brasil
Hi!

But are you using the NHibernate Contrib Nullable Types?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.