fiseni / PozitronDev.DIConfiguration

Extensions to .NET Core's built-in DI container, providing the ability for dynamic configuration.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

  NuGetNuGet

  Build Status

  Azure DevOps coverage

 

PozitronDev DIConfiguration

Nuget package providing extension methods to IServiceCollection (.NET Core's built-in DI infrastructure). The extensions provide the ability for dynamic/runtime DI configuration, through external config files.

I described the motivation and the use cases in the following blog post.

https://fiseni.com/posts/open-close-principle-and-runtime-di-configuration/

Usage

In order to simplify the usage, all bindings can be provided in the default configuration file "appsetting.json", within Bindings section. A sample console application can be found under sample folder within this repository.

Example configuration:

{
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Information",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "AllowedHosts": "*",

  "Bindings": {
    "binding1": {
      "service": "SampleLibrary.ISimpleService, SampleLibrary",
      "implementation": "SampleLibrary.SimpleService, SampleLibrary",
      "scope": "transient"
    },
    "binding2": {
      "service": "SampleLibrary.IGenericService`1, SampleLibrary",
      "implementation": "SampleLibrary.GenericService`1, SampleLibrary",
      "scope": "scoped"
    }
  }
}

The services and implementations should be provided as fully qualified type names, e.g. "Namespace.Type, AssemblyName".

The generic types can be provided through the standard notation, by adding `n suffix, where n is the number of generic paremeters (refer to the above example).

The scope options are scoped, transient, and singleton.

The configuration is being registered through the AddBindings extension method to IServiceCollection.

Example usage:

class Program
{
    static void Main(string[] args)
    {
        var host = Host.CreateDefaultBuilder()
                .ConfigureServices((context, services) =>
                {
                    services.AddBindings(context.Configuration);
                })
                .Build();


        using (var scope = host.Services.CreateScope())
        {
            var simpleService = scope.ServiceProvider.GetRequiredService<ISimpleService>();
            Console.WriteLine(simpleService.GetMessage());

            var genericService = scope.ServiceProvider.GetRequiredService<IGenericService<Object>>();
            Console.WriteLine(genericService.GetMessage());
        }
    }
}

Example ASP.NET Core usage:

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBindings(Configuration);
    }
}

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

About

Extensions to .NET Core's built-in DI container, providing the ability for dynamic configuration.

License:MIT License


Languages

Language:C# 100.0%