In my previous blog I showed the basics of using Type Converters in OpenAccess ORM. I even walked through creating a very basic type converter for storing an int as a varchar in SQL Server. The example converter was very basic, so in this blog I would like to take a look at something a little more practical.
In this example I will create a converter that tells OpenAccess ORM to store the value of an Enum property using the enum value’s name. Out of the box OpenAccess will persist enum’s using the enum’s underlying type which by default is int. In addition, the goal is to make the converter generic so that it can be reused for any enum. :)
As I mentioned, I want to make the converter usable for any enum so you can see that I have made the converter generic, and placed a struct constraint on it. System.Enum can not be used as a constraint, so the closest we can get is to make the generic constraint struct, and then check that the type is a valid enum during initialization.
Handling Converter Initialization
Here we need to initialize our type converter. In this case, I make sure to check that the we are actually dealing with an Enum, and if not an error will be thrown. This error will be thrown as soon as OpenAccess Initializes the type converter. Next we add a check to see if OpenAccess is looking for a converter for the type this converter handles, and if so we go ahead and return an instance of this type converter, otherwise we return null.
When reading the value we want to first check and see if it is null. If it is null we go ahead and return the value 0. If it is not null we parse the string that is stored in the database to get the actual Enum value. We then return this value.
To wire up the writing we tell OpenAccess that we are working with a string, and then we check to see if it has a value. If there is a valid value, we use Enum.GetName to get the string name for the Enum’s value. We then set the parameter size, and value.
The last part of the converter we need to set up tells OpenAccess how to handle the value when we are querying. In this case if the value is null, we tell OpenAccess to use SQL Null. Otherwise we return true, which tells OpenAccess to use the value in the field, but to wrap it in single quotes.
To use the converter all we need to do is specify it in our mapping configuration. As you can see I use the WithConverter<T> method, using EnumToStringConverter<> as the type, which in turn uses the DayOfWeek type.
The model object’s Day property is the DayOfWeek enum:
As you can see the enum’s name is properly persisted to the database.
Also, the Enum value is successfully retrieved when we load a record from the database.
We can also query the property as usually:
And we will get the expected results:
Here are the results of the query bound to RadGridView for WinForms:
I hope this helps you when working with Enum’s and OpenAccess. Once I wrap up this series I will post the code to all converters. In the next blog we will take a look at how we can easily work with images using these type converters.