In a previous blog I showed how to customize Telerik OpenAccess ORM data mappings using custom TypeConverters.  In particular we looked at storing, and querying, an enum by its name rather than its int value.  In this example, I will show how to get OpenAccess to serialize a class to xml, and store it in the database.  In the end you will have a reusable TypeConverter you can use to store any class as XML using OpenAccess! :)

Creating the Type Converter

The first thing we need to do is create the type converter.  To do that, simply grab the basic skeleton I displayed in this blog post.  Then implement the class as follows (see comments inline):

Set up the Data Model

Next configure the OpenAccess Data model to use this type converter for a property in the data model.  In this example I created a simple UserSettings class I want to store in the database as xml.

The class looks like this:

Now add a property on one of the objects in the data model using this new class:

The final step is to tell OpenAccess to use the new type converter to handle the database mapping for the new UserSettings Property.

Now we are ready to rock!


Now that the data model is all wired up,  the new type converter can be tested using the following code:

And the result as seen in the database!?


Since the data is stored as xml, we can click on it and see all the data stored in the column:


So the object is stored to the database as we wanted, but is the data retrieved correctly?


Indeed it is!


In this blog I showed how it is possible to create a custom type converter to store classes as XML using OpenAccess ORM.  Hopefully you find this useful!  In the attached code there are many other cool TypeConverter examples, feel free to play around with them.  Also, feel free to let me know any cool TypeConverters you make! :)

Download Source

Happy Coding!

About the Author

Ivailo Ivanov

 is Team Lead in Telerik Data Access

Related Posts