Skip to content

karthikveeraj/Microsoft.Extensions.DependencyInjection.AutoConfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microsoft.Extensions.DependencyInjection.AutoConfig

License Build Status NuGet

When you work on bigger projects, you need to write long list repetative lines of code to register interface types and their realizations. I wanted to avoid such piece of code and automate it so that we don't have to worry about registering specific types.

Instead of registering multiple types individually, AutoConfig provides an option to register the whole assembly itself which internatlly identifiers all the interfaces and their implementors and registers them. If there is a more than one implementation for a specific interface, it gives an option to mention which type you would like to resolve.

Installing NuGet package

Visual Studio:

PM> Install-Package KarthikVeeraj.Microsoft.Extensions.DependencyInjection.AutoConfig

.NET Core CLI:

dotnet add package KarthikVeeraj.Microsoft.Extensions.DependencyInjection.AutoConfig

Sample appSetings.config file

{
  "dependencyInjectionConfig": {
    "registerAssemblies": [
      { "order": 1, "type": "Transient", "name": "Microsoft.Extensions.DependencyInjection.AutoConfig.Tests.dll" }
    ],

    "registerTypes": [
      {
        "from": "Microsoft.Extensions.DependencyInjection.ITypeRegistration, Microsoft.Extensions.DependencyInjection.AutoConfig",
        "to":   "Microsoft.Extensions.DependencyInjection.TypeRegistration,  Microsoft.Extensions.DependencyInjection.AutoConfig",
        "type": "Singleton"
      },
      {
        "from": "Microsoft.Extensions.DependencyInjection.AutoConfig.Tests.IScopedMessage, Microsoft.Extensions.DependencyInjection.AutoConfig.Tests",
        "to":   "Microsoft.Extensions.DependencyInjection.AutoConfig.Tests.ScopedMessage,  Microsoft.Extensions.DependencyInjection.AutoConfig.Tests",
        "type": "Scoped"
      }
    ]
  }
}

Using AutoConfig

namespace Microsoft.Extensions.DependencyInjection.AutoConfig
{
    using System;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var configuration = new ConfigurationBuilder()
                                    .SetBasePath(AppContext.BaseDirectory)
                                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                    .Build() ?? throw new NullReferenceException("Failed to create 'Configuration' instance.");

                var provider = new ServiceCollection()
                                    .AddAutoTypeResolver(configuration) // <--- Use this statement for automatic registrations.
                                    .BuildServiceProvider() ?? throw new NullReferenceException("Failed to create the 'ServiceProvider' instance.");
                                    
                // Now, use provider here onwards to resolve registered types!
                // EXAMPLE 1 - Get auto registered types.
                var message = provider.GetService<IDemoMessage>();
                // EXAMPLE 2 - Get the specific type when multiple implementations available in the specified assembly. (By speficiying class name)
                var demoInstance = provider.GetService<IDemo>("Demo1");
            }
            catch (Exception ex)
            {
                // ... Handler error.
            }
        }
    }
}

License

Microsoft.Extensions.DependencyInjection.AutoConfig is open source software, licensed under the terms of MIT license. See LICENSE.txt for details.

About

AutoConfig provides an option to register the whole assembly which internatlly identifiers all the interfaces and their implementors and registers them automatically for you. If there are more than one implementation for a specific interface, it gives an option to mention which type you would like to resolve.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages