Alav.DI - additional Tools for Working with Dependency Injection. The tool allows you to inject service dependencies using annotations. This will allow you to set the lifetime, for example, directly in the service class itself. If you set the lifetime for an interface or an abstract class, then it will be applied automatically to all descendants.
Install the NuGet Package.
Install-Package Alav.DI
dotnet add package Alav.DI
The library adds extension method to IServiceCollection
:
Scan
- This is the entry point to set up your assembly scanning.
See Examples below for usage examples.
public interface IPingService
{
void Ping();
}
...
using Alav.DI.Attributes;
//Annotation for class inclusion in scan results
[ADI(Alav.DI.Enums.ADIServiceLifetime.Singleton, typeof(IPingService), typeof(PingService))]
public class PingService : IPingService
{
public void Ping()
{
Console.WriteLine($"{nameof(PingService)}:Ping");
}
}
...
using Alav.DI.Extensions;
...
var services = new ServiceCollection()
.Scan<Program>()
.BuildServiceProvider();
var pingService = services.GetService<IPingService>();
pingService.Ping();
ADIInjectAttribute
- this attribute marks the fields that need to be injected dynamically (without explicit declaration in the constructor)
[ADI(ADIServiceLifetime.Scoped, typeof(ITestService))]
public class TestService : ITestService
{
[ADIInject]
private readonly ILogger<TestService> _logger;
[ADIInject]
private readonly IPingService _pingService;
public void Ping()
{
_logger.LogInformation($"{nameof(TestService)}:Ping");
_pingService.Ping();
}
}