I've been using a pattern I found a while back where you use basic xml tags but wrap the settings in a static config class. So - a DIY App.Settings.
DotNetPearls Static Config Pattern
If you do it this way you can:
- use different sets of config values for different environments (dev, test, prod)
- provide for sensible defaults for each setting
- control how values are defined and instantiated
It's tedious to set up but performs well, hides references to key names, and is strongly typed. This kind of pattern works well for config that isn't changed by the application, although you could probably work in support for changes as well.
Config:
<add key="machineName" value="Prod" />
<add key="anotherMachineName" value="Test" />
<add key="EnvTypeDefault" value="Dev" />
<add key="RootURLProd" value="http://domain.com/app/" />
<add key="RootURLTest" value="http://test.domain.com/app/" />
<add key="RootURLDev" value="http://localhost/app/" />
<add key="HumanReadableEnvTypeProd" value="" />
<add key="HumanReadableEnvTypeTest" value="Test Mode" />
<add key="HumanReadableEnvTypeDev" value="Development Mode" />
Config class:
using System;
using System.Collections.Generic;
using System.Web;
using WebConfig = System.Web.Configuration.WebConfigurationManager;
public static class Config
{
#region Properties
public static string EnvironmentType { get; private set; }
public static Uri RootURL { get; private set; }
public static string HumanReadableEnvType { get; private set; }
#endregion
#region CTOR
/// <summary>
/// Initializes all settings when the app spins up
/// </summary>
static Config()
{
// Init all settings here to prevent repeated NameValueCollection lookups
// Can increase performance on high volume apps
EnvironmentType =
WebConfig.AppSettings[System.Environment.MachineName] ??
"Dev";
RootURL =
new Uri(WebConfig.AppSettings["RootURL" + EnvironmentType]);
HumanReadableEnvType =
WebConfig.AppSettings["HumanReadableEnvType" + Config.EnvironmentType] ??
string.Empty;
}
#endregion
}