LLBLGen only comes with a boolean type converter, but I wanted to map one of my int database fields to an enum in my project.
The first thing I did was create a new class library project in my solution and set the output directory of this project to be "C:\Program Files\Solutions Design\LLBLGen Pro v2.0\TypeConverters"
The main application project already contained the following enum (I renamed the namespaces, types and values to be more generic for this example. You will obviously want to replace YourNamespace and YourEnumType with more appropriate values):
namespace YourNamespace
{
public enum YourEnumType: int
{
ADefaultValue = 0,
AnotherValue = 1,
YouGetTheIdea = 2
}
}
To make this enum available to the type converter, you should add a reference from the main application project to the type converter project. (Truthfully, I don't actually know if adding this reference will work becasue I'm working on an ASP.NET v2 project and therefore can't make this reference... for the time being, I moved the enum above to my type converter project)
I then added a new class to the type converter project with the following code:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace YourNamespace.TypeConverters
{
public class YourTypeConverter : TypeConverter
{
public YourTypeConverter ()
{
}
public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
{
switch ( sourceType.FullName )
{
case "System.Int32":
case "System.Int16":
return true;
default:
return false;
}
}
public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType )
{
switch ( destinationType.FullName )
{
case "System.Int32":
case "System.Int16":
return true;
default:
return false;
}
}
public override object ConvertFrom( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value )
{
YourNamespace.YourEnumType yourEnum = YourNamespace.YourEnumType.ADefaultValue;
switch ( value.GetType().FullName )
{
case "System.Int32":
case "System.Int16":
yourEnum = (YourNamespace.YourEnumType)Enum.ToObject( typeof( YourNamespace.YourEnumType), (int)value );
break;
default:
throw new NotSupportedException( "Conversion from a value of type '" + value.GetType().ToString() + "' to YourNamespace.YourEnumType isn't supported" );
}
return status;
}
public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType )
{
if ( value == null )
{
throw new ArgumentNullException( "value", "Value can't be null" );
}
if ( !( value is YourNamespace.YourEnumType) )
{
throw new ArgumentException( "Value isn't of type YourNamespace.YourEnumType", "value" );
}
YourNamespace.YourEnumType yourEnum = (YourNamespace.YourEnumType)value;
switch ( destinationType.FullName )
{
case "System.Int32":
return (int)yourEnum;
case "System.Int16":
return (short)yourEnum;
default:
throw new NotSupportedException( "Conversion to a value of type '" + destinationType.ToString() + "' isn't supported" );
}
}
public override object CreateInstance( ITypeDescriptorContext context, System.Collections.IDictionary propertyValues )
{
return YourNamespace.YourEnumType.ADefaultValue;
}
}
}
Now, make sure LLBLGen is shutdown and compile your solution. If successful, you should be able to open LLBLGen again and set any of your int fields to use the type converter. To do this, right click on an entity in LLBLGen and select "Edit / Properties". Then, on the "Mapped entity fields" tab, select a field and look below for the "Use different .NET type" drop down list. This drop down list should contain an entry for your type converter. Select it and press the "Set" button.
Generate that sucker and switch back to your VS solution. To make everything work right, you've got to add a reference from your type converter project to both your DatabaseSpecific and DatabaseGeneric projects (Note, I'm using the adapter framework in my solution and both the specific and generic projects are part of my solution.)
That's it! You should be able to compile the your LLBLGen projects again and use your entities with the enum attached to them. It works great and was easy to do! In fact, it took three times longer to write this post than it did to create and test the type converter!
Have fun and let me know if you run into any problems. I wrote most of this from memory after I finished, so it's possible I forgot something.