Skip to content

Hantse/DependencyInjection.Autoregister

Repository files navigation

DependendyInjection.Autoregister

Dependency injection autoregistration with attribute

Package Build Version
DependencyInjection.Autoregister.Abstraction Build Status 0.0.1-preview02
DependencyInjection.Autoregister.Providers.Autofac
DependencyInjection.Autoregister.Providers.ServiceCollection Build Status 0.0.1-preview02-071219
DependencyInjection.Autoregister.Providers.Unity

DependencyInjection.Autoregister.Abstraction

Provide an helper and attribute. Helper return all type must be register and load in specific assembly, after you can implement your own registration system.

Install package

NuGet version

Package Manager

PM> Install-Package DependencyInjection.Autoregister.Abstraction -Version 0.0.1-preview02

DotNet CLI

dotnet add package DependencyInjection.Autoregister.Abstraction --version 0.0.1-preview02

Use attribute for register

Simple Usage

[DependencyRegistration]
public class SecondBusiness
{
    public string GetValueUppercase(string value)
    {
        return value.ToUpperInvariant();
    }
}

Simple Usage with lifetime

[DependencyRegistration(ServiceRegistrationType.SINGLETON)]
public class SecondBusiness
{
    public string GetValueUppercase(string value)
    {
        return value.ToUpperInvariant();
    }
}

Simple Usage with name

[DependencyRegistration("MyName")]
public class SecondBusiness
{
    public string GetValueUppercase(string value)
    {
        return value.ToUpperInvariant();
    }
}

Simple Usage with interface

public interface IThirdBusiness
{
    string GetValueUppercase(string value);
}

[DependencyRegistration]
public class ThirdBusiness : IThirdBusiness
{
    public string GetValueUppercase(string value)
    {
        return value.ToUpperInvariant();
    }    
}

Simple Usage with multiple interface

public interface IThirdBusiness
{
    string GetValueUppercase(string value);
}

public interface ISecondBusiness
{
    string GetValueLower(string value);
}

[DependencyRegistration]
public class ThirdBusiness : IThirdBusiness, ISecondBusiness
{ 
    public string GetValueUppercase(string value)
    {
        return value.ToUpperInvariant();
    } 

    public string GetValueLower(string value)
    {
        return value.ToLowerInvariant();
    }     
}

DependencyInjection.Autoregister.Providers.ServiceCollection

It's a implementation for autoregister on IServiceCollection provider.

Install package

NuGet version

Package Manager

PM> Install-Package DependencyInjection.Autoregister.Providers.ServiceCollection -Version 0.0.1-preview02-071219

DotNet CLI

dotnet add package DependencyInjection.Autoregister.Providers.ServiceCollection --version 0.0.1-preview02-071219

Autoregister on ASPNET Core

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddAutoRegister();
    }
}

Autoregister without ASPNET Core

var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
services.AddAutoRegister(Assembly.GetAssembly(typeof(MyAssemblyName)));
var provider = services.BuildServiceProvider();
var resolveService = provider.GetService<FirstClass>();

About

Dependency injection autoregistration with attribute

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages