Skip to content

kevbite/HumbleConfig

Repository files navigation

HumbleConfig

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.

install from nugetdownloads Build status

Installing packages

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

How to use it?

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");

Key formatters

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.

KeyPrefixer

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:")
KeyPostfixer

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")

Key formatters on sources

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.

Contributing

  1. Fork
  2. Hack!
  3. Pull Request.