Skip to content

Environment-based typed configuration provider with default JSON configuration reader

License

Notifications You must be signed in to change notification settings

iiwaasnet/config-provider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build status NuGet version

TypedConfigProvider

During its life-cycle, software is deployed on multiple environments: developer's machine, test stage, production servers, etc. Each environment may require different configuration to be provided for the software to work properly. TypedConfigProvider makes management of environment-dependent configuration simple:

  • create default configuration section
  • provide only needed overrides for each of the environments
  • set the environment sequence, i.e. order in which configuration sections should be applied

How to start

Let's assume, for the below configuration the sequence of environments is "prod, dev":

// Sample.config.json
{
    "prod": {
        "stringProp": "value",
        "intProp": 1
    },
    "dev": {
        "stringProp": "dev-override-value"
    }
}

Final configuration, available for application, will look like the following:

{
    "stringProp": "dev-override-value",
    "intProp": 1
}

Environment sequence may be provided, for instance, via app.config or Web.config files. AppConfigTargetProvider class implements reading of the environment sequence from app.config file.

<appSettings>
  <!-- override PROD settings with DEV ones  -->
  <add key="JsonConfig.EnvironmentSequence" value="PROD, DEV" />
</appSettings>

Application configuration class for the example above should look like this:

public class SampleConfiguration
{
  public string StringProp {get; set;}
  public int IntProp {get; set;}
}

Naming and other conventions

Default directory to search for configuration files - config subfolder of the main executable's directory. You can override it by providing a list of directories to search for via ConfigFileLocator ctor.

All configuration classes should have their names ending with Configuration suffix. Corresponding config files should be named without Configuration suffix and have extension *.config.json:

  • Class name => MyApplicationConfiguration
  • File name => MyApplication.config.json

Sequence of configuration sections in *.config.json file is not important, as they are applied based on provided environment sequence.

How to handle array properties in configuration

There are some tricks though with array properties. Let's assume the following environment sequence "prod, dev" for the sample below:

{
    "prod": {
        "urls": [
            "http://server1.prod",
            "http://server2.prod"
        ]
    },
    "dev": {
        "urls": [
            "http://server.dev"
        ]
    }
}

Final configuration will contain all three urls configured: two from "prod" section and one from "dev" section. If this was not your intention, you may introduce kind of ”reset” (name could be any) section to fix this:

{
    "prod": {
        "urls": [
            "http://server1.prod",
            "http://server2.prod"
        ]
    },
    "dev": {
        "urls": [
            "http://server.dev"
        ]
    },
    "reset": {
        "urls": null
    }
}

Sequence "prod, reset, dev" gives the following resulting configuration:

{
    "urls": [
       "http://server.dev"
    ]
}

Please, look into Tests project for more use cases.

About

Environment-based typed configuration provider with default JSON configuration reader

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published