Giter Club home page Giter Club logo

nimbleconfig's Introduction

NimbleConfig Build status NuGet Downloads

A simple, unambitious, convention-based configuration injector for .NET using IConfiguration (Microsoft.Extensions.Configuration) with full support for AspNetCore.


Getting Started

  1. Install and reference the Nuget NimbleConfig.DependencyInjection.Aspnetcore

In the NuGet Package Manager Console, type:

    Install-Package NimbleConfig.DependencyInjection.Aspnetcore
  1. Define your settings class as follows
    // Our setting is a string
    public class SomeSetting: ConfigurationSetting<string>
    {
    }
	
    // or for a more complex type
	
    public class SomeComplexSetting : IComplexConfigurationSetting
    {
        public string SomeProperty { get; set; }
    }
  1. Add it to your appsettings.json
    {
        "SomeSetting": "SomeValue",
        "SomeComplexSetting": {
            "SomeProperty": "SomeValue"
        }
    }
  1. Inject and use it in your controllers, services etc
    public class ValuesController : ControllerBase
    {
        private readonly SomeSetting _someSetting;
        private readonly SomeComplexSetting _someComplexSetting;
		
        public ValuesController(SomeSetting someSetting, SomeComplexSetting someComplexSetting)
        {
            _someSetting = someSetting;
            _someComplexSetting = someComplexSetting;
        }
		
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { 
                _someSetting.Value,
                _someComplexSetting.SomeProperty
            };
        }
    }
  1. In the ConfigureServices() method in your Startup.cs add the following to scan and inject settings types
    public void ConfigureServices(IServiceCollection services)
    {
        // Other services go here
		
        // Wire it up using the fluent api
        services.AddConfigurationSettings().AndBuild();
    }

You can try this if you have to access some configuration setting prior to setting up the DI container. (Be warned! This will create a instance of a factory for each call. Only do this if there is no other way.)

    // You still need to provide an instance of IConfiguration
    var dirtySetting = configuration.QuickReadSetting<SomeSetting>();

Want more?

See the sample projects for more advanced use cases like complex types, enums and arrays. Checkout the ConsoleApp example on how to use it in a non aspnetcore app.

NimbleConfig provides full customisation of the setting creation via lifetime hooks in IConfigurationOptions. This is done via creating your own resolvers for the name (IKeyName), reader (IConfigurationReader), parser (IParser), constructor (IValueConstructor).

Example of setting a prefix uisng the configuration options lifetime hooks

    var configOptions = ConfigurationOptions.Create()
                            .WithGlobalPrefix("MyAppSettings:") // Adding a global prefix to key names
                            .WithNamingScheme((type, name) => // Resolving type specific key names
                            {
                                if (type == typeof(SomeSetting)) // selectively apply logic
                                {
                                    return new KeyName("AnotherPrefix", name.QualifiedKeyName);
                                }
                         
                                return name; // return the auto-resolved one if no change is needed
                            });
    
    // Then just pass it in to the builder uisng the fluent api
	
    services.AddConfigurationSettings()
            .UsingOptionsIn(configOptions)
            .AndBuild();

These fluent apis allow you to easily add your custom logic. They take a function which accepts a type and the auto-resolved instance as seen in the above example.

  • .WithNamingScheme() for setting configuration key names.
  • .WithReader() for setting a custom config reader.
  • .WithParser() for setting a custom parser.
  • .WithConstructor() for setting a custom value constructor.

Feel free to contribute and raise issues as you see fit :)

nimbleconfig's People

Contributors

andymac4182 avatar dasiths avatar yashints avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nimbleconfig's Issues

Add Support For Structs

Support ConfigurationSetting<SomeStruct> the same way support for complex types/arrays are done.

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.