DynamicDI 1.0.1

dotnet add package DynamicDI --version 1.0.1                
NuGet\Install-Package DynamicDI -Version 1.0.1                
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="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicDI --version 1.0.1                
#r "nuget: DynamicDI, 1.0.1"                
#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.
// Install DynamicDI as a Cake Addin
#addin nuget:?package=DynamicDI&version=1.0.1

// Install DynamicDI as a Cake Tool
#tool nuget:?package=DynamicDI&version=1.0.1                

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 DynamicDI

Or using .NET CLI:

dotnet add package DynamicDI

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

  1. The library scans all project assemblies for classes marked with RegisterService.
  2. It retrieves their implemented interfaces and registers them based on the defined lifetime.
  3. 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 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. 
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
1.0.1 44 2/7/2025
1.0.0 9 2/6/2025

Added support for .NET 7.0 and .NET 8.0