Created
December 6, 2012 16:41
-
-
Save wilsonat/4225905 to your computer and use it in GitHub Desktop.
NHibernate user type to trim char(n) columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TrimmedString : IUserType | |
{ | |
public bool IsMutable | |
{ | |
get { return false; } | |
} | |
public Type ReturnedType | |
{ | |
get { return typeof (string); } | |
} | |
public SqlType[] SqlTypes | |
{ | |
get { return new SqlType[1] { new SqlType(DbType.AnsiStringFixedLength) }; } | |
} | |
public object Assemble(object cached, object owner) | |
{ | |
return this.DeepCopy(cached); | |
} | |
public object DeepCopy(object value) | |
{ | |
if (value == null) | |
{ | |
return (object) null; | |
} | |
else | |
{ | |
return (object) string.Copy((string) value); | |
} | |
} | |
public object Disassemble(object value) | |
{ | |
return this.DeepCopy(value); | |
} | |
public bool Equals(object x, object y) | |
{ | |
if (x == null) | |
{ | |
return y == null; | |
} | |
else | |
{ | |
return x.Equals(y); | |
} | |
} | |
public int GetHashCode(object x) | |
{ | |
return x.GetHashCode(); | |
} | |
public object NullSafeGet(IDataReader rs, string[] names, object owner) | |
{ | |
string str = (string) NHibernateUtil.String.NullSafeGet(rs, names[0]); | |
return str != null ? (object) str.Trim() : (object) (string) null; | |
} | |
public void NullSafeSet(IDbCommand cmd, object value, int index) | |
{ | |
if (value == null) | |
{ | |
NHibernateUtil.String.NullSafeSet(cmd, (object) null, index); | |
} | |
else | |
{ | |
value = (object) ((string) value).Trim(); | |
NHibernateUtil.String.NullSafeSet(cmd, value, index); | |
} | |
} | |
public object Replace(object original, object target, object owner) | |
{ | |
return original; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment