DynamicDI 2.0.0
dotnet add package DynamicDI --version 2.0.0
NuGet\Install-Package DynamicDI -Version 2.0.0
<PackageReference Include="DynamicDI" Version="2.0.0" />
<PackageVersion Include="DynamicDI" Version="2.0.0" />
<PackageReference Include="DynamicDI" />
paket add DynamicDI --version 2.0.0
#r "nuget: DynamicDI, 2.0.0"
#:package DynamicDI@2.0.0
#addin nuget:?package=DynamicDI&version=2.0.0
#tool nuget:?package=DynamicDI&version=2.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
and RegisterDbContext
attributes.
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 DynamicDI
Or using .NET CLI:
dotnet add package DynamicDI
Usage
1. Mark Services with the Attribute
Apply the RegisterService
or RegisterDbContext
attributes to classes:
[RegisterService] // transient by default
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", "!" ];
}
}
[RegisterDbContext]
public class DataContext : DbContext
{
}
2. Register Services
Modify the Program.cs
file to call the RegisterServices()
and RegisterDbContexts()
extension methods:
using DynamicDI;
var builder = WebApplication.CreateBuilder(args);
builder.Services.RegisterServices(); // services registration
builder.Services.RegisterDbContexts(); // db contexts registration
You can also specify the assemblies from where you want to register services:
using DynamicDI;
Assembly[] assemblies = GetAssemblies(); // your assemblies
builder.Services.RegisterServices(assemblies);
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;
}
}
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 | net7.0 is compatible. 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 is compatible. 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 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. 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. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.20)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyModel (>= 7.0.0)
-
net8.0
- Microsoft.EntityFrameworkCore (>= 7.0.20)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyModel (>= 7.0.0)
-
net9.0
- Microsoft.EntityFrameworkCore (>= 7.0.20)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyModel (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
added documentation + added the ability to specify assemblies where to get services from