GithubHelp home page GithubHelp logo

forki / reactive.config Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kevm/reactive.config

0.0 3.0 0.0 105 KB

WIP. Reactive configuration from any source you care about.

License: MIT License

Batchfile 0.40% F# 1.68% C# 97.91%

reactive.config's Introduction

Reactive Configuration

Build status

Reactive Configuration let's you inject type safe configuraion settings into your application's types which can be source one or more Configuration Sources. These sources know how to keep the settings up-to-date using Observables under the hood, hence Reactive Configuration.

Currently out of the box we provide:

  • StructureMap IoC container integration
  • JSON configuration source

Example

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

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 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 currently don't have a configuration file so we are getting the default properties for the configured type. If you add this JSON file to your current working directory you can override the configuration

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.Connfig

./build.cmd

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

reactive.config's People

Contributors

kevm avatar

Watchers

Steffen Forkmann avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.