Stop writing boiler plate configuration code to pull from different sources.
HumbleConfig allows developers to concentrate on writing the application instead of managing all the configuration locations.
PM> Install-Package HumbleConfig
PM> Install-Package HumbleConfig.ConfigurationManager
PM> Install-Package HumbleConfig.EnvironmentVariables
PM> Install-Package HumbleConfig.ConfigR
PM> Install-Package HumbleConfig.MongoDb
PM> Install-Package HumbleConfig.Credstash
PM> Install-Package HumbleConfig.Caching
First, create an Configuration
instance:
var configuration = new Configuration();
Then, configure the sources for configuration:
configuration.AddEnvironmentVariables()
.AddConfigurationManager()
.AddConfigR()
.AddMongoDb("mongodb://localhost/settings", "appSettings")
.WithDefaultMemoryCache(TimeSpan.FromHours(1))
.AddCredstash(RegionEndpoint.EUWest1)
.WithCache(MemoryCache.Default, () => new CacheItemPolicy())
.GetConfiguration();
The order in which we add the configuration sources will determine which configuration values will take priority, in the above example environment variables will override any configuration values within mongodb.
Now knowing the priority of configuration values, we can add some default values by using a InMemory source:
var defaults = new Dictionary<string, object>() { {"UserName", "Kevin.Smith"} };
configuration.AddInMemory(defaults);
Once we're happy with our configuration we can pull out an app setting:
var value = await configuration.GetAppSettingAysnc<string>("key");
Ever been in config hell where you don't know what key is used where. This is where key formatters comes in useful, HumbleConfig has inbuilt support for a few key formatters.
The key prefixer allows you to specify a prefix that all your config keys should include.
For example having a prefix of HumbleConfig:
would have the following output:
Key | Source Key |
---|---|
Key1 | HumbleConfig:Key1 |
Key2 | HumbleConfig:Key2 |
Key3 | HumbleConfig:Key3 |
To setup this the key prefixer on our configuration object we just call WithKeyPrefixer
:
configuration.WithKeyPrefixer("HumbleConfig:")
The key postfixer allows you to specify a postfix that all your config keys should include.
For example having a postfix of .HumbleConfig
would have the following output:
Key | Source Key |
---|---|
Key1 | Key1.HumbleConfig |
Key2 | Key2.HumbleConfig |
Key3 | Key3.HumbleConfig |
To setup this the key postfixer on our configuration object we just call WithKeyPostfixer
:
configuration.WithKeyPostfixer(".HumbleConfig")
It is also possible to use key formatter on individual sources, for example:
configuration.AddEnvironmentVariables().WithKeyPostfixer(".production")
This will only apply the key formatter to the environment variables source.
- Fork
- Hack!
- Pull Request.