One option for you is to use a Configuration object to contain default values for any configuration settings. You could then, in the constructor for the configuration object, attempt to override the default config values with values from somewhere such as app.config, a database configuration table, etc.
This is useful for a number of purposes:
- Unit testing tools (such as nUnit) require special configuration to access app.config.
- The existance of app.config file is not necessary as it will use the default values (which seems to be your goal).
- Can help ensure type-safe configuration values.
A simple example:
public class MyConfiguration
{
private string _defaultValue1 = "Value1";
private int _defaultValue2 = 22;
public string Value1
{
get
{
return _defaultValue1;
}
}
public int Value2
{
get
{
return _defaultValue2;
}
}
#region cnstr
public MyConfiguration()
{
LoadValuesFromConfigurationXml();
}
#endregion
public static MyConfiguration GetConfig()
{
// Optionally Cache the config values in here... caching code removed
// for simplicity
return new MyConfiguration();
}
internal void LoadValuesFromConfigurationXml()
{
int tempInt;
string value = ConfigurationManager.AppSettings["Value1"];
if (!String.IsNullOrEmpty(value))
{
_defaultValue1 = value;
}
value = ConfigurationManager.AppSettings["Value2"];
if (!String.IsNullOrEmpty(value))
{
if (int.TryParse(value, out tempInt))
{
_defaultValue2 = tempInt;
}
}
}
}
To access the config values use: MyConfiguration.GetConfig().Value1
Hope this helps,
Max