Skip to content

Instantly share code, notes, and snippets.

@lukesmith
Created June 15, 2010 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukesmith/439031 to your computer and use it in GitHub Desktop.
Save lukesmith/439031 to your computer and use it in GitHub Desktop.
[Serializable]
public class EmailAddressType : IUserType
{
public SqlType[] SqlTypes
{
get
{
var types = new SqlType[1];
types[0] = new SqlType(DbType.String);
return types;
}
}
public Type ReturnedType
{
get { return typeof(EmailAddress); }
}
public new bool Equals(object x, object y)
{
return x != null && x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (!string.IsNullOrEmpty(value))
{
return new EmailAddress(value);
}
return null;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null || !(value is EmailAddress))
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
}
else
{
var emailAddress = (EmailAddress)value;
NHibernateUtil.String.NullSafeSet(cmd, emailAddress, index);
}
}
public object DeepCopy(object value)
{
return value;
}
public bool IsMutable
{
get { return false; }
}
public object Replace(object original, object target, object owner)
{
//As our object is immutable we can just return the original
return original;
}
public object Assemble(object cached, object owner)
{
//Used for casching, as our object is immutable we can just return it as is
return cached;
}
public object Disassemble(object value)
{
//Used for casching, as our object is immutable we can just return it as is
return value;
}
}
[Serializable]
public class PasswordType : IUserType
{
public SqlType[] SqlTypes
{
get
{
var types = new SqlType[2];
types[0] = new SqlType(DbType.String);
types[1] = new SqlType(DbType.String);
return types;
}
}
public System.Type ReturnedType
{
get { return typeof(Password); }
}
public new bool Equals(object x, object y)
{
if (x == null)
{
return false;
}
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
var saltValue = (string)NHibernateUtil.String.NullSafeGet(rs, names[1]);
if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(saltValue))
{
var result = new Password
{
Value = value,
Salt = saltValue
};
return result;
}
return null;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null || !(value is Password))
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
NHibernateUtil.String.NullSafeSet(cmd, null, index + 1);
}
else
{
var password = (Password)value;
NHibernateUtil.String.NullSafeSet(cmd, password.Value, index);
NHibernateUtil.String.NullSafeSet(cmd, password.Salt, index + 1);
}
}
public object DeepCopy(object value)
{
if (value == null)
{
return null;
}
if (value is Password)
{
var original = (Password)value;
var result = new Password
{
Value = original.Value,
Salt = original.Salt
};
return result;
}
return null;
}
public bool IsMutable
{
get { return false; }
}
public object Replace(object original, object target, object owner)
{
//As our object is immutable we can just return the original
return original;
}
public object Assemble(object cached, object owner)
{
//Used for casching, as our object is immutable we can just return it as is
return cached;
}
public object Disassemble(object value)
{
//Used for casching, as our object is immutable we can just return it as is
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment