Skip to content

KevM/Reactive.Config

Repository files navigation

Reactive Configuration

Build status

Reactive Configuration let's you inject strongly typed configuration into your application sourced from one or more Configuration Sources. These sources keep your configuration up-to-date using Observables. If your observed configuration changes the next time you "need" your settings they will be up-to-date, hence Reactive Configuration.

Philosophy

The goal of this library is to be your application's core supplier of static and dynamic configuration management. With dynamic configuration, we can build a lot of nice things on top of this:

Today

Currently out of the box we provide:

Application Settings Example

Lets take a look at using Reactive.Config. To make a configurable type simply add the IConfigurable marker interface to the type.

public class MyConfigured : IConfigured
{
    public bool IsEnabled { get; set; } = true;
    public DateTime EnabledOn { get; set; } = DateTime.UtcNow + TimeSpan.FromDays(-7);
    public string MyAppKey { get; set; } = "sooooouuuuuper seeeecrette app key";
}

Next we'll take a constructor dependency on this configured type in one of our application types.

public class MyService
{
    private readonly MyConfigured _config;

    public MyService(MyConfigured config)
    {
        _config = config;
    }

    public void DoSomething()
    {
        if (!_config.IsEnabled || DateTime.Now <= _config.EnabledOn)
        {
            return;
        }

        //login using my appkey
        Console.WriteLine($"Logging in using AppKey: {_config.MyAppKey}");
    }
}

Next, we'll setup our IoC container to use the JSON configuration source.

In this example we use StructureMap. Here we scan the current assembly for types needing reactive configuration.

var container = new Container(_ =>
{
    _.ReactiveConfig(rc =>
    {
        rc.AddSource<JsonConfigurationSource>();
    });

    _.Scan(s =>
    {
        s.TheCallingAssembly();
        s.Convention<ReactiveConfig>();
    });
});
var service = container.GetInstance<MyService>()
service.DoSomething(); 

Run this code in a console app and you see this printed out: Logging in using AppKey: sooooouuuuuper seeeecrette app key.

Changing the configuraion:

We have not added JSON configuration file to our project. We get no errors just a default instance of the configured type.

Now, let's add this JSON file to your current working directory.

MyConfigured.json

{
  "MyAppKey" : "a different app key"
}

Run the app again and you see Logging in using AppKey: a different app key.

Building

Open a command line:

git clone https://github.com/KevM/Reactive.Config

cd Reactive.Config

./build.cmd

The build automation will pull down any dependencies, build, and run all tests.

Continuous Integration

AppVeyor is kind enough to provide CI for this project. Thanks!