While developing .NET applications, we always look for configuration values stored in either Web.config or App.config. We can simply use the <appSettings>…</appSettings> section which is converted to a dictionary or key/value pair object. Can we use a strongly-typed object for the configuration? Yes, we can. With the <configSections>…</configSections> node, we can convert configuration values into strongly-typed objects. This is a very useful feature in most cases. However, what if we have requirements like:

  • I want to convert a string value into an enum value, regardless of the case sensitivity.
  • I want to convert a comma delimited string value into an array (or list), regardless of the case sensitivity.
  • I want to convert a pipe delimited string value into a flag decorated enum value, regardless of the case sensitivity.

Those questions should be sorted out by ourselves. In this post, we will have a look how to convert a string value into a different type we want to use, particularly answering the question above.

The sample source code used here can be found at https://github.com/aliencube/Configuration-Value-Converter

Sample Configuration Section

Assuming that we have a Web.config with the following config section.

The <converterSettings>…</converterSettings> section will be converted into a strongly typed object. Let’s have a look.

CaseInsensitiveEnumConverter

Let’s say there is an enum object called ProductStatus that has two values Active and Inactive.

The built-in converter, EnumConverter or GenericEnumConverter, takes only case-sensitive string values. Therefore, if we pub ACTIVE or active into Web.config, this won’t be converted. How can we convert those values, then? Let’s create a CaseInsensitiveEnumConverter for it.

It basically inherits the ConfigurationConverterBase class and override the ConvertFrom method. Within the method, just parse the string value to a desired enum type. Then decorate a property with this like:

Sweet! We now can convert a string value into an enum value.

CommaDelimitedListConverter

This time, we’re going to convert a comma delimited string values into a list using a decorator like above. Let’s see the code bits.

Same as above, by inheriting the ConfigurationConverterBase class and overriding the ConvertFrom method, a given string value can be easily converted to a List object. Decorate a property with this like:

With this decorator, the comma delimited string value is now converted to a List in this example.

PipeDelimitedFlaggedEnumConverter

Let’s say we have a flagged enum called ProductTypes that contains ProductA, ProductB and ProductC like:

Look at the following code bits for flagged enum object.

Inherit the ConfigurationConverterBase class and override the ConvertFrom method. This time, spot on how the flagged enum value is converted. As we pass TEnum as a generic type, we don’t actually know if this is flagged enum or not. However, we know this is the flagged enum value. Therefore, we simply convert all the parsed values into integer ones and add them up, then convert the final integer value into the flagged enum. Once implemented, decorate this to a configuration property like:

Now, we have a pipe delimited string value converted into a flagged enum value.


So far, we have briefly walked through how we can implement TypeConverter for our purpose. These techniques are not that hard to implement but quite useful when they are frequently used in our project. This is also published into NuGet, so download and use it for your convenience.

PS. This is not compatible with .NET Core applications.

Category:
Application Development and Integration
Tags:
, ,

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

%d bloggers like this: