Skip to content

Expand native dependency injection with property and field injection

License

Notifications You must be signed in to change notification settings

Antelcat/DependencyInjection.Autowired

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Antelcat.DependencyInjection.Autowired

Extensions of native .NET dependency injection with Autowired, provides a way to support properties and fields injection.

All lifetimes and generics are now supported. And using ILGeneratorEx to speed up the setter.

Usage

public class Service{
    [Autowired]
    private readonly IService dependency;
    [Autowired]
    private IService Dependency { get; set; }
}

In common :

IServiceProvider provider = new ServiceCollection()
                            .Add(...)
                            .BuildAutowiredServiceProvider(static x=>x.BuildServiceProvider());
IService service = provider.GetService<IService>();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers() //register controllers
                .AddControllersAsServices() // add controllers as services
                .UseAutowiredControllers(); // use auto wired controllers
builder.Host.UseAutowiredServiceProviderFactory(); // autowired services

Tests could be found in ServiceTest.cs , which shows higher performance than Autofac and is close to native.

Migration

Meanwhile, you can still use your attribute, only need to provide it at build time :

IServiceProvider provider = collection.BuildAutowiredServiceProvider<YourAutowiredAttribute>(...);
builder.Services.AddControllers() 
                .AddControllersAsServices()
                .UseAutowiredControllers<YourAutowiredAttribute>(); 
builder.Host.UseAutowiredServiceProviderFactory<YourAutowiredAttribute>();