Building custom configuration section handlers
Been playing around with creating my own configuration section handlers. Call me crazy, but i just have a distaste for storing my hierarchical application configurations in some kind of artificially derived flat Key/Value pair scheme.
I see a lot of applications doing something like:
<AppSettings>
<add key="MyApplication.SomeValue" value="foo" />
<add key="MyApplication.DB.Host" value="localhost" />
<add key="MyApplication.DB.Password" value="secret" />
</AppSettings>
when it would be so much cleaner to do:
<my_application>
<some_value>foo</some_value>
<db>
<host>localhost</host>
<password>secret</password>
</db>
</my_application>
The answer to this is System.Configuration.IConfigurationSectionHandler and then you stuff the result in a singleton. A couple of pitfalls with this approach are:
Application vs. ASP.NET: The singleton needs to determine whether it's running as an application or under ASP.NET, so it can decide whether it needs to be a thread singleton or can just be a static variable. Easiest solution here is to allow a way to manually initialize the singleton with a flag. Then you can use Begin_Request in global.asax to tell it to use a thread singleton and default to a static variable otherwise
Allowing for config relocation like AppSettings: AppSettings lets you externalize your name/value pairs via