I have implement a IUserType to provide solution for mapping for char with length more than one
If you find an improvement or Bug in this code. Please send me and email
Code:
/// <summary>
/// Summary description for MultipleCharType.
/// </summary>
public class MultipleCharType:NHibernate.IUserType
{
private static readonly bool IsDebugEnabled;
private static NullableType multipleChar = NHibernate.NHibernateUtil.String;
static MultipleCharType()
{
//cache this, because it was a significant performance cost
IsDebugEnabled = LogManager.GetLogger( typeof( IType ).Namespace ).IsDebugEnabled;
}
private ILog Log
{
get { return LogManager.GetLogger( GetType() ); }
}
public MultipleCharType()
{
}
#region IUserType Members
public new bool Equals(object x, object y)
{
if(x==y) return true;
string lhs = (x==null) ? null : (string)x;
string rhs = (y==null) ? null : (string)y;
return multipleChar.Equals(lhs, rhs);
}
public NHibernate.SqlTypes.SqlType[] SqlTypes
{
get
{
return new SqlType[] { multipleChar.SqlType };
}
}
public object DeepCopy(object value)
{
return value;
}
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if(value.Equals(null))
{
if( IsDebugEnabled )
{
Log.Debug( "binding null to parameter: " + index.ToString() );
}
( (IDbDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
if( IsDebugEnabled )
{
Log.Debug( "binding '" + value.ToString() + "' to parameter: " + index );
}
//Change the DBtype because the find query will not work
IDataParameter parm = cmd.Parameters[ index ] as IDataParameter;
parm.DbType=System.Data.DbType.AnsiStringFixedLength;
parm.Value = value;
}
}
public Type ReturnedType
{
get { return typeof(System.String); }
}
public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
int index = rs.GetOrdinal( names[0] );
if( rs.IsDBNull( index ) )
{
if( IsDebugEnabled )
{
Log.Debug( "returning null as column: " + names[0] );
}
return null;
}
else
{
string charValue =(string)multipleChar.NullSafeGet(rs, names);
if( IsDebugEnabled )
{
Log.Debug( "returning '" + charValue + "' as column: " + names[0] );
}
return charValue.TrimEnd(' ');
}
}
public bool IsMutable
{
get { return multipleChar.IsMutable; }
}
#endregion
}