Giter Site home page Giter Site logo

cfgdotnet's Introduction

CfgDotNet

CfgDotNet allows you to manage your configuration data in json for multiple environments. Simply create a "cfg.json" file in the root of your project in this format:

{
    "activeEnvironment": "dev",
    "environments": {
        "dev": {
            "connectionStrings": {
                "MainConnection": {
                    "connectionString": "server=dev.databaseserver.com;database=MyDB_DEV;uid=User_DEV;pwd=pa55w0rd!_DEV",
                    "providerName": "System.Data.SqlClient"
                }
            },
            "appSettings": {
                "showDebugPanel": true
            },
            "elasticsearch": {
                "user": "elastic-user-dev",
                "password": "0d35e633c2cf40a78fb6537294a084f8_dev",
                "baseUrl": "https://dev.fakeelasticserver.com:9200",
                "defaultIndex": "dev_documents"
            }
        },
        "prod": {
            "connectionStrings": {
                "MainConnection": {
                    "connectionString": "server=prod.databaseserver.com;database=MyDB_PROD;uid=User_PROD;pwd=pa55w0rd!_PROD",
                    "providerName": "System.Data.SqlClient"
                }
            },
            "appSettings": {
                "showDebugPanel": false
            },
            "elasticsearch": {
                "user": "elastic-user-prod",
                "password": "847d80d268e842dd9ff4562c3117417c_prod",
                "baseUrl": "https://prod.fakeelasticserver.com:9200",
                "defaultIndex": "prod_documents"
            }
        }
    }
}

You can provide a BaseEnvironment and just override the necessary values in other environments:

{
    "activeEnvironment": "prod",
    "baseEnvironment": "dev",
    "environments": {
        "dev": {
            "connectionStrings": {
                "MainConnection": {
                    "connectionString": "server=dev.databaseserver.com;database=MyDB_DEV;uid=User_DEV;pwd=pa55w0rd!_DEV",
                    "providerName": "System.Data.SqlClient"
                }
            },
            "appSettings": {
                "alwaysTheSame": "yep",
                "showDebugPanel": true
            },
            "elasticsearch": {
                "user": "elastic-user",
                "password": "0d35e633c2cf40a78fb6537294a084f8_dev",
                "baseUrl": "https://fakeelasticserver.com:9200",
                "defaultIndex": "documents"
            }
        },
        "prod": {
            "connectionStrings": {
                "MainConnection": {
                    "connectionString": "server=prod.databaseserver.com;database=MyDB_PROD;uid=User_PROD;pwd=pa55w0rd!_PROD"
                }
            },
            "appSettings": {
                "showDebugPanel": false
            },
            "elasticsearch": {
                "password": "847d80d268e842dd9ff4562c3117417c_prod"
            }
        }
    }
}

You can also use the built-in SettingsManager which makes it easy to use strongly-typed settings classes in your code. These classes can be populated from the app.config or web.config AppSettings or ConnectionStrings sections, also, cfg.json and Databases are supported. You can just add whichever providers you want, load the settings, optionally validate, then store in an ioc container. The mandatory ISettings interface includes a Validate() method that can throw exceptions if, for example, a database isn't available allowing your application to fail-fast on startup. Providers can even depend on settings populated by other providers (eg SqlDatabaseSettingsProvider can pull its ConnectionString)

const string environment = "prod";
const string cfgPath = "cfg.json";
var assemblies = new[] { GetType().Assembly };

new SettingsManager()
    .AddProvider(new ConnectionStringsSettingsProvider())
    .AddProvider(new AppSettingsSettingsProvider())
    .AddProvider(new CfgDotNetSettingsProvider(environment, cfgPath))
    .AddSettings(assemblies)
    .AddProvider<Settings>(x => new SqlDatabaseSettingsProvider(x.ConnectionString))
    .LoadSettingsFromProviders()
    .Validate()
    .ForEach(x => Container.RegisterSingle(x.GetType(), x));

cfgdotnet's People

Contributors

mjlefevre avatar pwhe23 avatar

Stargazers

Ganeshan Venkataraman avatar Nigel Finley avatar Dmytro Zharii avatar Adam Ferguson avatar  avatar

Watchers

 avatar James Cloos avatar

Forkers

pwhe23

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.