Skip to content

Instantly share code, notes, and snippets.

@jholt456
Created October 4, 2011 22:20
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 jholt456/1263017 to your computer and use it in GitHub Desktop.
Save jholt456/1263017 to your computer and use it in GitHub Desktop.
Enum2String Type Converter
public class EnumToStringConverter<TEnum> : Telerik.OpenAccess.Data.AdoTypeConverter where TEnum : struct
{
/// <summary>
/// Reads data from the data reader into the type
/// </summary>
/// <param name="holder">A place where the read data can be stored unboxed</param>
/// <returns>Value that was read or <c>null</c> when boxing is turned off</returns>
public override object Read(ref DataHolder holder)
{
return null;
}
/// <summary>
/// Writes a value to the given parameter potentially avoiding boxing.
/// </summary>
/// <param name="holder">A place where the value is stored and where the parameter
/// can be obtained from.</param>
public override void Write(ref DataHolder holder)
{
}
/// <summary>
/// Generates the SQL string that represents the given value.
/// </summary>
/// <param name="holder">A place where the value is stored and where the result is
/// stored
/// in the <see cref="F:Telerik.OpenAccess.Data.DataHolder.StringValue" /> field.</param>
/// <returns><c>True</c> when the generated SQL string needs to be quoted.</returns>
public override bool CreateLiteralSql(ref DataHolder holder)
{
return false;
}
/// <summary>
/// Gets the default CLR type that this converter is converting to; must be nullable.
/// </summary>
/// <value>Type that is handled per default on this converter.</value>
public override Type DefaultType
{
get { return typeof(TEnum); }
}
}
public override AdoTypeConverter Initialize(IDataColumn user, Type clr, IAdoTypeConverterRegistry registry, bool secondaryTable)
{
if (!DefaultType.IsEnum)
throw new System.ArgumentException(
typeof(TEnum).Name +
" is not an Enum");
if (clr == typeof(TEnum))
{
return base.Initialize(user, clr, registry, secondaryTable);
}
return null;
}
myClassConfiguration.HasProperty(x => x.Day)
.ToColumn("Day")
.HasColumnType("varchar")
.WithConverter<EnumToStringConverter<DayOfWeek>>();
public class MyClass
{
public int ID { get; set; }
public DayOfWeek Day { get; set; }
public int MyIntStoredInString { get; set; }
public Image Image { get; set; }
public UserSettings Settings { get; set; }
public string Name { get; set; }
public string Test { get; set; }
}
public override bool CreateLiteralSql(ref DataHolder holder)
{
if (holder.NoValue)
{
holder.StringValue = this.NullValueSql;
return false;
}
else
{
return true; // inidicating that ' are needed around, because it is a character column (VARCHAR)
}
}
public override object Read(ref DataHolder holder)
{
bool isNull = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = isNull;
if (isNull)
{
holder.ObjectValue = 0;
}
else
{
string value = holder.Reader.GetValue(holder.Position).ToString();
holder.ObjectValue = Enum.Parse(typeof(TEnum), value);
}
return (holder.Box) ? holder.ObjectValue : null;
}
var day = DayOfWeek.Sunday;
var results = context.GetAll<MyClass>().Where(c => c.Day == day);
public override void Write(ref DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if (!holder.NoValue)
{
string s = Enum.GetName(typeof(TEnum), holder.ObjectValue);
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment