Complex Data in .Net .config Files

The .Net framework and Visual Studio allows developers to simply edit configuration data using the Settings editor. This is pretty straight-forward, supports built-in data types and default values, and distinguishes between application and user settings.

The Settings editor also generates a Settings class which provides access via the Settings.Default instance to the typed values in the .config section.

There are a couple of drawbacks, though.

If you define settings in different libraries, and want to use these Settings classes, each Settings class is generated within the default namespace of each library. This makes it necessary to include all the section handlers of the various libaries in the application’s .config file.

Additionally, if you create, edit or delete of a library’s Settings file, the changes are applied only to the library’s .config file, and are not propagated to the .config files of all other projects referencing these settings.

Most importantly (for me, at least) is the lack of any structured or hierarchical way to arrange the settings properties. If you want to add settings describing the access to a web service (URL, username, password), then these 3 strings are stored in the Settings and .config without any obvious relation to each other.

Of course .Net provides a mechanism to store and retrieve structured data in .config files by means of the ConfigurationSection and ConfigurationElement classes.

Fortunately I found a Visual Studio add-in called Configuration Section Designer. CSD allows you to design your config section in a VS diagrammer in terms of sections and elements (i.e. XML element and attributes).

To use CSD, you need to create a separate project of type Configuration Section Project (under Visual C#, General templates).

For each configuration element, the developer defines the name, data type, whether it is required, and its default value. CSD lets you define validators on elements, so checking a config value for email or URL syntax by regular expressions is pretty easy.

Be aware though that if you supply a default value for an element, the validator must also explicitly accept the default value, otherwise initialization will fail during application startup.

CSD generates a .cs file containing the ConfigurationSections, ConfigurationElements and the C# attributes required for validation.

It also generates a stub .config file containing the declaration of the config section handler and the config section resulting from the diagram.

To use the configuration section and its handler in your application and libraries, simply add a reference to the CSD project and copy both lines from the CSD project’s .config to your app.config.

One Response to Complex Data in .Net .config Files

  1. […] Complex Data in .Net .config Files mentioned the built-in validators in the System.Configuration namespace as subclasses of ConfigurationValidatorAttribute. These validators are invoked as soon as the configuration section is accessed. […]

Leave a Reply

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

You are commenting using your 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: