Tolitech.DependencyInjection.Extensions 1.0.0-preview.4

This is a prerelease version of Tolitech.DependencyInjection.Extensions.
dotnet add package Tolitech.DependencyInjection.Extensions --version 1.0.0-preview.4
                    
NuGet\Install-Package Tolitech.DependencyInjection.Extensions -Version 1.0.0-preview.4
                    
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="Tolitech.DependencyInjection.Extensions" Version="1.0.0-preview.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tolitech.DependencyInjection.Extensions" Version="1.0.0-preview.4" />
                    
Directory.Packages.props
<PackageReference Include="Tolitech.DependencyInjection.Extensions" />
                    
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 Tolitech.DependencyInjection.Extensions --version 1.0.0-preview.4
                    
#r "nuget: Tolitech.DependencyInjection.Extensions, 1.0.0-preview.4"
                    
#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 Tolitech.DependencyInjection.Extensions@1.0.0-preview.4
                    
#: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=Tolitech.DependencyInjection.Extensions&version=1.0.0-preview.4&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tolitech.DependencyInjection.Extensions&version=1.0.0-preview.4&prerelease
                    
Install as a Cake Tool

Tolitech.DependencyInjection.Extensions

Tolitech.DependencyInjection.Extensions is a lightweight and modern .NET library that simplifies the discovery and automatic registration of interfaces and implementations (services, repositories, etc.) in the Dependency Injection (DI) container. It eliminates the manual work of mapping each interface to its implementation, making DI setup cleaner, more productive, and safer.

Features

  • Automatic registration of services, repositories, and dependencies via assembly scanning.
  • Support for Transient, Scoped, and Singleton lifetimes.
  • Native integration with Microsoft.Extensions.DependencyInjection.
  • Focus on performance and simplicity.

Installation

Via NuGet Package Manager:

Install-Package Tolitech.DependencyInjection.Extensions

Or via .NET CLI:

dotnet add package Tolitech.DependencyInjection.Extensions

How It Works

The library exposes extension methods for IServiceCollection that scan an assembly and automatically register all interface implementations inherited from a base type (e.g., IService, IRepository).

Available Methods

  • ScanAndAddTransient<TBase>(Assembly assembly)
  • ScanAndAddScoped<TBase>(Assembly assembly)
  • ScanAndAddSingleton<TBase>(Assembly assembly)

Each method registers all implementations of interfaces that inherit from TBase found in the specified assembly.

Usage Example

Suppose you have the following interfaces and implementations:

// Base interface
internal interface IService { }

// Specific interface
internal interface ICategoryService : IService
{
    bool IsOK();
}

// Implementation
internal sealed class CategoryService : ICategoryService
{
    public bool IsOK() => true;
}

To automatically register all services implementing IService:

using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Tolitech.DependencyInjection.Extensions;

var services = new ServiceCollection();
Assembly assembly = Assembly.GetExecutingAssembly();

// Register all services implementing IService as Scoped
services.ScanAndAddScoped<IService>(assembly);

// Resolve via DI
var provider = services.BuildServiceProvider();
var categoryService = provider.GetService<ICategoryService>();

The same applies to repositories or any other base interface pattern.

Complete Example

using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Tolitech.DependencyInjection.Extensions;

var services = new ServiceCollection();
Assembly assembly = Assembly.GetExecutingAssembly();

// Transient
services.ScanAndAddTransient<IRepository>(assembly);

// Scoped
services.ScanAndAddScoped<IService>(assembly);

// Singleton
services.ScanAndAddSingleton<IService>(assembly);

How It Works Internally

The library uses reflection to:

  • Find all concrete classes in the specified assembly.
  • Identify all implemented interfaces that inherit from the specified base type.
  • Register each found interface with its corresponding implementation in the desired lifetime.

Example method signature:

public static IServiceCollection ScanAndAddScoped<T>(this IServiceCollection services, Assembly assembly)

Automated Tests

The library includes unit tests using xUnit, ensuring the correct operation of automatic registration methods for all lifetimes (Transient, Scoped, Singleton).

Test example:

[Fact]
public void AddScoped_AddServiceCorrectly()
{
    var builder = new HostApplicationBuilder();
    Assembly assembly = Assembly.GetExecutingAssembly();

    builder.Services.ScanAndAddScoped<IService>(assembly);
    var provider = builder.Services.BuildServiceProvider();
    var categoryService = provider.GetService<ICategoryService>();

    Assert.NotNull(categoryService);
    Assert.True(categoryService.IsOK());
}

When to Use

  • Projects with many services/repositories and interface conventions.
  • APIs, web applications, microservices, and modular systems.
  • Situations where productivity and DI standardization are essential.

Advantages

  • Reduces repetitive code and manual registration errors.
  • Facilitates project maintenance and evolution.
  • Follows .NET Dependency Injection best practices.
Product 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.  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
1.0.0-preview.4 115 7/17/2025
1.0.0-preview.3 115 7/14/2025
1.0.0-preview.2 123 7/2/2025
1.0.0-preview.1 144 12/7/2024