Dependency injection autoregistration with attribute
Package | Build | Version |
---|---|---|
DependencyInjection.Autoregister.Abstraction | 0.0.1-preview02 | |
DependencyInjection.Autoregister.Providers.Autofac | ||
DependencyInjection.Autoregister.Providers.ServiceCollection | 0.0.1-preview02-071219 | |
DependencyInjection.Autoregister.Providers.Unity |
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.
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
[DependencyRegistration]
public class SecondBusiness
{
public string GetValueUppercase(string value)
{
return value.ToUpperInvariant();
}
}
[DependencyRegistration(ServiceRegistrationType.SINGLETON)]
public class SecondBusiness
{
public string GetValueUppercase(string value)
{
return value.ToUpperInvariant();
}
}
[DependencyRegistration("MyName")]
public class SecondBusiness
{
public string GetValueUppercase(string value)
{
return value.ToUpperInvariant();
}
}
public interface IThirdBusiness
{
string GetValueUppercase(string value);
}
[DependencyRegistration]
public class ThirdBusiness : IThirdBusiness
{
public string GetValueUppercase(string value)
{
return value.ToUpperInvariant();
}
}
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();
}
}
It's a implementation for autoregister on IServiceCollection provider.
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
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAutoRegister();
}
}
var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
services.AddAutoRegister(Assembly.GetAssembly(typeof(MyAssemblyName)));
var provider = services.BuildServiceProvider();
var resolveService = provider.GetService<FirstClass>();