Skip to content

A simple, unambitious, convention-based configuration injector for .NET with full support for Aspnetcore

License

Notifications You must be signed in to change notification settings

dasiths/NimbleConfig

Repository files navigation

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 :)

About

A simple, unambitious, convention-based configuration injector for .NET with full support for Aspnetcore

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages