Summary

At its core, strong.config generates a façade that lets you access your application's configuration values from a strongly-typed class instead of weakly-typed ConfigurationManager calls.

Test Drive

If you'd like to give strong.config a test drive before using it, you can download the ASP.NET MVC project that I develop it in from the "Source Code" page. Check out HomeController.cs and of course the web.config to see how it is implemented.

Implementation

  1. Download the latest release from the "Downloads" page (it's a single T4 file) and add it to the project of your choice.
  2. Complete the two TODOs in the T4, those being:
    1. Set the ConfigPath variable to the relative path of your configuration file (line 17 in version 1.0.0). It points to a Web.config file located in the project root by default.
    2. Change the namespace from StrongConfig.Example to match your project (line 22 in version 1.0.0).
  3. Run the T4.
You can now access all your configuration values via the static ConfigurationFacade class.

Features

Automatic Generation

As strong.config is a T4 file, the façade is generated automatically. Any changes to your configuration file will be mapped to the façade, and any modifications that need to be made to the rest your code will be signalled to you by the compiler. No more null value surprises at runtime!

Type Detection

strong.config is coded to detect the type of each configuration value by trial-and-error parsing. The following types are supported (and detected in this order):
  1. DateTime
  2. bool
  3. int
  4. string

This means that a configuration value of "true" will be parsed as a Boolean and not as a string. I may add functionality later on to override this if there is a real need expressed by the community for such a feature. I may also add support for more types, again, if there is a real need expressed by the community for such a feature. Both of these were left out from this release for the sake of simplicity.

XML Documentation Support

The final comment line before each element in the config file is automatically transformed into a <summary> documentation block for the element's property in the façade class.

For example, this config code:

<!-- The boolean key value. -->
<add key="BooleanKey" value="true"/>
becomes the following in the façade:

/// <summary>
/// The boolean key value.
/// </summary>
public static bool BooleanKey
{
    get { return bool.Parse(ConfigurationManager.AppSettings["BooleanKey"]); }
}
Commenting the configuration file is therefore strongly recommended.

Final Word

This project was born out of personal necessity, so while it meets my own needs quite nicely I would really appreciate any feedback on how it can be improved for wider public use. Feel free to leave me a comment or a note on the "Discussions" page!

Last edited Jun 2, 2013 at 7:24 PM by levi_botelho, version 3

Comments

No comments yet.