Custom Type Converters

One of the new features in Version 3.1 is support for custom type converters. This is actually the internals that allowed for the XDocument support in 3.0.3 being made public.

In order to create a custom type converter, the interface you need to implement is MicroLite.TypeConverters.ITypeConverter.

ITypeConverter

As you can see, the interface is quite simple only requiring 3 methods.

The CanConvert method allows the converter to tell MicroLite if it can handle conversion of the specified type. This will be called by MicroLite when it is trying to resolve the correct type converter to use to convert a value.

The ConvertFromDbValue method will be called by MicroLite when building an object from a result set, the value will be the object returned by the underlying DbDataReader and the type will be the property type (or object type if include scalar or execute scalar has been called).

The ConvertToDbValue method will be called by MicroLite when building an insert/update statement. The value will be the property value and the type will be the property type.

Once you have created an ITypeConverter, simply register it when you configure MicroLite:

using MicroLite.TypeConverter;

TypeConverter.Converters.Add(new MyCustomTypeConverter());
Advertisements

4 thoughts on “Custom Type Converters

    1. trevorpilley Post author

      Yep, anything you can fit into a database column type, you can create a custom converter for a .NET object (either a native one or one you create). Have a look in the repository to see how the XDocument one works.

      Reply
  1. Pingback: MicroLite Comparison Feedback | MicroLite ORM

  2. Pingback: MicroLite 5.0 – Changes to Type Converters | MicroLite ORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s