In this post, I will describe a simple method of achieving strongly typed application settings from your app config source (app.config, web.config, appSettings.config). There are many ways out there to achieve this, but having dived into Fubu MVC for over a year, I grew fond of the way they handle application settings through a class instead of using ConfigurationManager or a custom config section. In addition, even though the code below is not what you would find in the Fubu source, it is inspired and based on it. So, the credit for the idea and the basic implementation belongs to the folks who actively contribute to it. Finally, this post is based on StructureMap but I am sure that you can achieve the same effect using your favorite DI tool.

Purpose: To rid your code of the soup of ConfigurationManager calls and Magic Strings that is all too common in code.

The composition of this feature is very simple and is exclusively handled by StructureMap.

By inspecting the console code, we see a bootstrapping call.  In this call, we see the initialization block for StructureMap, with an additional custom convention called SettingsScanner.

SettingsScanner registers all the types in the assembly that end with name ‘Settings,’ setting all of its public properties with their matching appSettings values.

Get the entire code from GitHub


https://github.com/LeoJH/StronglyTypedAppSettings

Food for thought


One important feature missing from this simple implementation is support for addional types; specifically, integers. Currently, this will only support string types which is what we get by default from .Net.