wis.nuget.auto.dependencyInjection
1.0.2
dotnet add package wis.nuget.auto.dependencyInjection --version 1.0.2
NuGet\Install-Package wis.nuget.auto.dependencyInjection -Version 1.0.2
<PackageReference Include="wis.nuget.auto.dependencyInjection" Version="1.0.2" />
<PackageVersion Include="wis.nuget.auto.dependencyInjection" Version="1.0.2" />
<PackageReference Include="wis.nuget.auto.dependencyInjection" />
paket add wis.nuget.auto.dependencyInjection --version 1.0.2
#r "nuget: wis.nuget.auto.dependencyInjection, 1.0.2"
#:package wis.nuget.auto.dependencyInjection@1.0.2
#addin nuget:?package=wis.nuget.auto.dependencyInjection&version=1.0.2
#tool nuget:?package=wis.nuget.auto.dependencyInjection&version=1.0.2
AutoServiceRegistration
AutoServiceRegistration simplifies the registration of services in ASP.NET Core's Dependency Injection (DI) container by automatically discovering and registering services based on custom attributes.(Works fine in .Net 6.0)
Usage
Step 1: Register Services in the DI Container
Use the AutoRegisterServices extension method to automatically register your services in the Startup.cs file:
var builder = WebApplication.CreateBuilder(args);
... //Other builder
// Auto-register services
builder.Services.AutoRegisterServices();
// Other configuration methods
}
Step 2: Decorate Your Services
Decorate your service implementations with the [Service], specifying the service lifetime and priority if needed. This version add option for lifetime and priority
public interface IMyService
{
void DoWork();
}
[Service(ServiceLifetime.Transient, priority: 1)]
public class FirstImplementation : IMyService
{
public void DoWork()
{
Console.WriteLine("First Implementation");
}
}
In case there is no option for life time default value will be generate:
ServiceLifetime lifetime = ServiceLifetime.Transient, int priority = 0
Step 3: Resolve and Use Services
Resolve your services as usual via dependency injection in your controllers or other service consumers:
public class MyController : ControllerBase
{
private readonly IMyService _myService;
public MyController(IMyService myService)
{
_myService = myService;
}
public IActionResult DoWork()
{
_myService.DoWork(); // Output: "First Implementation"
return Ok();
}
}
Bonus
I also make it can be used with [Lombok.NET] for both controller and service so if you want you can declare like this
[Inside Controller]
[RequiredArgsConstructor]
public partial class MyController : ControllerBase
{
private readonly IMyService _myService;
//function apply same as above
}
[Inside Service]
[Service]
[RequiredArgsConstructor]
public partial class MyService: IMyService
{
private readonly IMyRepository _myRepository;
public void DoWork()
{
Console.WriteLine("First Implementation"); // Output: "First Implementation"
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.