DynamicDI 2.0.0

dotnet add package DynamicDI --version 2.0.0
                    
NuGet\Install-Package DynamicDI -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicDI" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DynamicDI" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DynamicDI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DynamicDI --version 2.0.0
                    
#r "nuget: DynamicDI, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DynamicDI@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DynamicDI&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DynamicDI&version=2.0.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 149 8/14/2025
1.1.0 118 8/11/2025
1.0.2.1 128 4/11/2025
1.0.1 105 2/7/2025
1.0.0 91 2/6/2025

added documentation + added the ability to specify assemblies where to get services from