A simple configuration wrapper to abstract config sources and offer sensible defaults.
Available on Nuget
.net 4.0 +
The IConfigWrapper provides the contract for reading config values.
The IWritableConfigWrapper provides the contract for reading and writing values.
In this example, we return 1000 if there is no configured value in the app.config or web.config file.
IConfigWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
In this example, we return an array of strings from the Windows registry, delimited by , or |.
If we have no values for the specified key, we get the array ["pork", "beans"]
IConfigWrapper = new WindowsRegistryConfigWrapper();
var items = configWrapper.Get<string[]>("HKCU/topkey/subkey/valuekey", new [] {"pork", "beans"}, []{',''|'});
By default, the neither the AppSettingsConfigWrapper nor the WindowsRegistryConfigWrapper throws an exception if a config has a value of the wrong type. If it cannot cast, it returns the default.
Example: for a config entry
<appSettings>
<add key="sleep-time-in-ms" value="chicken sandwich"/>
</appSettings>
and the code
IConfigWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
// sleepMs = 1000
We try to cast the value "chicken sandwich" to an int. This fails, so we return 1000.
To throw an exception, pass in true for errorOnWrongType.
IConfigWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000, true);
//throws an Exception
If you do not pass in a default, it will error on missing values or values that cannot be cast.
IConfigWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("this-key-is-not-in-the-config");
// throws an exception
Each config wrapper loads from a different source.
Loads from the current application's app.config or web.config
<appSettings>
<add key="sleep-time-in-ms" value="5000"/>
</appSettings>
IConfigWrapper configWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
// sleepMs = 5000
Loads from the Windows registry, defining a root key for my application.
IWritableConfigWrapper = new WindowsRegistryConfigWrapper("HKLM/Software/MyApplication/");
configWrapper.Set<int>("MyKey", 5000);
var sleepMs = configWrapper.Get<int>("MyKey", 5000);
// sleepMs = 5000
You can also give it fully-qualified key names as well, but it will use the canonnical name of the root key: e.g. HKCU becomes HKEY_CURRENT_USER.
IWritableConfigWrapper = new WindowsRegistryConfigWrapper("HKLM/Software/MyApplication/");
configWrapper.Set<int>("HKEY_LOCAL_MACHINE/Software/MyApplication/MyKey", 5000);
var sleepMs = configWrapper.Get<int>("HKEY_LOCAL_MACHINE/Software/MyApplication/MyKey", 5000);
// sleepMs = 5000
While the WindowsRegistryConfigWrapper will not modify root level keys e.g. "`", it should be used with care. It is a sharp knife, be careful.
Supports ini files with values like
myvalue=foo
The json wrapper is in a separate nuget package: ConfigWrapper.Json. It has a dependency on Newtonsoft, so it is distributed separately from ConfigWrapper.
IConfigWrapper configWrapper = new JsonConfigWrapper("../data.json");
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 5000);
// sleepMs = 5000
The in memory wrapper is useful for unit test scenarios or where we want to hold onto a config temporarily.
See the list of contributors who participated in this project.
This project is licensed under the MIT License
- Hat tip to the teams at Spotlite (RallyHealth), Guaranteed Rate, GrubHub, and Endava for their feedback.