DynamicDI 1.0.0
See the version list below for details.
dotnet add package DynamicDI --version 1.0.0
NuGet\Install-Package DynamicDI -Version 1.0.0
<PackageReference Include="DynamicDI" Version="1.0.0" />
paket add DynamicDI --version 1.0.0
#r "nuget: DynamicDI, 1.0.0"
// Install DynamicDI as a Cake Addin #addin nuget:?package=DynamicDI&version=1.0.0 // Install DynamicDI as a Cake Tool #tool nuget:?package=DynamicDI&version=1.0.0
Dynamic DI Registration Library
Overview
This library provides automatic service registration in the ASP.NET Core Dependency Injection container by scanning assemblies and detecting classes marked with a RegisterService
attribute.
It simplifies and streamlines the process of service registration, reducing manual configurations and improving maintainability.
Features
✔ Automatic Registration – Detects and registers services dynamically based on attributes.
✔ Flexible Interface Binding – Supports registering either the first implemented interface or all interfaces.
✔ Configurable Lifetimes – Allows specifying service lifetimes (Transient, Scoped, Singleton).
✔ Assembly Scanning – Automatically discovers and registers services from all project assemblies.
✔ Zero Boilerplate Code – Eliminates the need for manual AddTransient, AddScoped, or AddSingleton calls.
Installation
Install via NuGet Package Manager:
Install-Package DIalect
Or using .NET CLI:
dotnet add package DIalect
Usage
1. Mark Services with the Attribute
Apply the RegisterService
attribute to service classes:
[RegisterService(ServiceLifeCycle.Transient, InterfaceRegistrationStrategy.AllInterfaces)]
public class TestService(ITestRepository repository) : ITestService, ITestable
{
private readonly ITestRepository _repository = repository;
public string GetHelloMessage() => "Hello World!";
public List<string> GetMessages() => _repository.GetMessages();
public bool IsThisATest() => true;
}
[RegisterService(ServiceLifeCycle.Singleton)]
public class TestRepository : ITestRepository
{
public List<string> GetMessages()
{
return [ "Hello", "World", "!" ];
}
}
2. Register Services
Modify the Program.cs
file to call the RegisterServices
extension method:
using DIalect;
var builder = WebApplication.CreateBuilder(args);
builder.Services.RegisterServices();
var app = builder.Build();
3. Inject Services Anywhere
Once registered, services can be injected as usual:
public class MyController : ControllerBase
{
private readonly IUserService _userService;
public MyController(IUserService userService)
{
_userService = userService;
}
}
How It Works
- The library scans all project assemblies for classes marked with
RegisterService
. - It retrieves their implemented interfaces and registers them based on the defined lifetime.
- Services become available in the DI container without manual registration.
License
This library is open-source and licensed under the MIT License. Contributions and feedback are welcome! 🚀
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.1)
- Microsoft.Extensions.DependencyModel (>= 9.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.