Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/antelcat/dependencyinjection.autowired
Expand native dependency injection with property and field injection
https://github.com/antelcat/dependencyinjection.autowired
aop aspnetcore autowired dependency-injection extension field-injection ioc property-injection reflection
Last synced: 3 months ago
JSON representation
Expand native dependency injection with property and field injection
- Host: GitHub
- URL: https://github.com/antelcat/dependencyinjection.autowired
- Owner: Antelcat
- License: mit
- Created: 2023-05-31T02:34:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-30T02:11:25.000Z (about 1 year ago)
- Last Synced: 2024-03-26T15:03:59.988Z (10 months ago)
- Topics: aop, aspnetcore, autowired, dependency-injection, extension, field-injection, ioc, property-injection, reflection
- Language: C#
- Homepage:
- Size: 63.5 KB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Antelcat.DependencyInjection.Autowired
Extensions of native [.NET dependency injection](https://github.com/dotnet/docs/blob/main/docs/core/extensions/dependency-injection.md) with [Autowired](https://github.com/Antelcat/Antelcat.Shared/blob/main/Antelcat.Shared/Antelcat.Shared.DependencyInjection.Autowired/Attributes/AutowiredAttribute.cs), provides a way to support properties and fields injection.
All lifetimes and generics are now supported. And using [ILGeneratorEx](https://github.com/Antelcat/ILGeneratorEx) to speed up the setter.
## Usage
``` c#
public class Service{
[Autowired]
private readonly IService dependency;
[Autowired]
private IService Dependency { get; set; }
}
```## In common :
``` c#
IServiceProvider provider = new ServiceCollection()
.Add(...)
.BuildAutowiredServiceProvider(static x=>x.BuildServiceProvider());
IService service = provider.GetService();
```## In [ASP.NET CORE MVC](https://github.com/dotnet/aspnetcore) :
```c#
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](./src/Antelcat.DependencyInjection.Autowired.Test/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 :
```c#
IServiceProvider provider = collection.BuildAutowiredServiceProvider(...);
builder.Services.AddControllers()
.AddControllersAsServices()
.UseAutowiredControllers();
builder.Host.UseAutowiredServiceProviderFactory();
```