Skip to content

Instantly share code, notes, and snippets.

@suryagaddipati
Created August 8, 2010 18:37
Show Gist options
  • Select an option

  • Save suryagaddipati/514396 to your computer and use it in GitHub Desktop.

Select an option

Save suryagaddipati/514396 to your computer and use it in GitHub Desktop.
public class EnumMappingType : IEnhancedUserType , IParameterizedType
{
private Type enumClass;
public string Name
{
get { return "enumstring - " + enumClass.Name; }
}
public Type ReturnedClass
{
get { return enumClass; }
}
public object StringToObject(string xml)
{
return null;
}
public object FromXMLString(string xml)
{
throw new NotImplementedException();
}
public string ObjectToSQLString(object value)
{
return GetValue(value).ToString();
}
public virtual object GetValue(object code)
{
//code is an enum instance.
return code == null ? string.Empty : code.ToString();
}
public void SetParameterValues(IDictionary parameters)
{
enumClass = Type.GetType((string) parameters["enumClass"],true);
}
public object FromStringValue(string xml)
{
return GetInstance(xml);
}
public virtual object GetInstance(object code)
{
//code is an named constants defined for the enumeration.
try
{
return Enum.Parse(enumClass, code as string, true);
}
catch (ArgumentException ae)
{
throw new HibernateException(string.Format("Can't Parse {0} as {1}", code, enumClass.Name), ae);
}
}
public string ToString(object value)
{
return (value == null) ? null : GetValue(value).ToString();
}
public bool Equals(object x, object y)
{
return (x == y) || (x != null && y != null && x.Equals(y));
}
public int GetHashCode(object x)
{
throw new NotImplementedException();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
object code = rs[rs.GetOrdinal(names[0])];
if (code == DBNull.Value || code == null)
{
return null;
}
return GetInstance(code);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
IDataParameter par = (IDataParameter)cmd.Parameters[index];
if (value == null)
{
par.Value = DBNull.Value;
}
else
{
par.Value = Enum.Format(this.enumClass, value, "G");
}
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.GetString(10) }; }
}
public Type ReturnedType
{
get { return enumClass; }
}
public bool IsMutable
{
get {return false; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment