ConfigWrapper 1.2.0
See the version list below for details.
dotnet add package ConfigWrapper --version 1.2.0
NuGet\Install-Package ConfigWrapper -Version 1.2.0
<PackageReference Include="ConfigWrapper" Version="1.2.0" />
<PackageVersion Include="ConfigWrapper" Version="1.2.0" />
<PackageReference Include="ConfigWrapper" />
paket add ConfigWrapper --version 1.2.0
#r "nuget: ConfigWrapper, 1.2.0"
#:package ConfigWrapper@1.2.0
#addin nuget:?package=ConfigWrapper&version=1.2.0
#tool nuget:?package=ConfigWrapper&version=1.2.0
ConfigWrapper
A simple configuration wrapper to abstract config sources and offer sensible defaults.
Getting Started
Available on Nuget
Prerequisites
.net 4.0 +
Use
Simple values
var configWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
In this example, we return 1000 if there is no configured value in the app.config or web.config file.
Simple values
var configWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
In this example, we return 1000 if there is no configured value in the app.config or web.config file.
Arrays
var configWrapper = new AppSettingsConfigWrapper();
var items = configWrapper.Get<string[]>("sample-items", new [] {"pork", "beans"}, []{',''|'});
In this example, we return an array of strings from the config, delimited by , or |.
If we have no values in config we get the array ["pork", "beans"]
Exceptions
By default, the AppSettingsConfigWrapper does not throw an exception if a config has a value of the wrong type: it returns the default.
For a config entry:
<appSettings>
<add key="sleep-time-in-ms" value="chicken sandwich"/>
</appSettings>
var configWrapper = 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, fail and return 1000.
To throw an exception, pass in true for errorOnWrongType.
var configWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000, true);
//throws an Exception
Sources
Each config wrapper loads from a different source.
AppSettingsConfigWrapper
Loads from the current application's app.config or web.config
<appSettings>
<add key="sleep-time-in-ms" value="5000"/>
</appSettings>
var configWrapper = new AppSettingsConfigWrapper();
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 1000);
// sleepMs = 5000
WindowsRegistryConfigWrapper
var configWrapper = new WindowsRegistryConfigWrapper();
configWrapper.Set<int>("HKLM/MyApplication/MyKey", 5000);
var sleepMs = configWrapper.Get<int>("sleep-time-in-ms", 5000);
// sleepMs = 5000
WARNING
While the WindowsRegistryConfigWrapper will not modify root level keys e.g. "HKLM.", it can be used to damage your system and should be used with care.
Authors
See the list of contributors who participated in this project.
License
This project is licensed under the MIT License
Acknowledgments
- Hat tip to the teams at Spotlite (RallyHealth) and Guaranteed Rate where I wrote earlier versions of these concepts.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConfigWrapper:
Package | Downloads |
---|---|
ConfigWrapper.Json
Helper classes to make it easier to work with json config files. Part of the ConfigWrapper project. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.8.0 | 1,514 | 7/26/2018 |
1.7.1 | 994 | 7/26/2018 |
1.7.0 | 1,008 | 7/25/2018 |
1.6.0 | 1,174 | 7/20/2018 |
1.5.0 | 1,197 | 6/19/2018 |
1.4.1 | 1,234 | 6/12/2018 |
1.4.0 | 1,593 | 6/11/2018 |
1.3.1 | 1,213 | 6/7/2018 |
1.3.0 | 1,570 | 6/6/2018 |
1.2.1 | 1,015 | 5/25/2018 |
1.2.0 | 1,219 | 5/24/2018 |
1.1.0 | 1,251 | 5/24/2018 |
1.0.1 | 1,183 | 5/23/2018 |
1.0.0 | 1,355 | 5/23/2018 |
Add WindowsRegistryConfigWrapper to use the Windows registry as a config source.