Tolitech.DependencyInjection.Extensions
1.0.0-preview.4
dotnet add package Tolitech.DependencyInjection.Extensions --version 1.0.0-preview.4
NuGet\Install-Package Tolitech.DependencyInjection.Extensions -Version 1.0.0-preview.4
<PackageReference Include="Tolitech.DependencyInjection.Extensions" Version="1.0.0-preview.4" />
<PackageVersion Include="Tolitech.DependencyInjection.Extensions" Version="1.0.0-preview.4" />
<PackageReference Include="Tolitech.DependencyInjection.Extensions" />
paket add Tolitech.DependencyInjection.Extensions --version 1.0.0-preview.4
#r "nuget: Tolitech.DependencyInjection.Extensions, 1.0.0-preview.4"
#:package Tolitech.DependencyInjection.Extensions@1.0.0-preview.4
#addin nuget:?package=Tolitech.DependencyInjection.Extensions&version=1.0.0-preview.4&prerelease
#tool nuget:?package=Tolitech.DependencyInjection.Extensions&version=1.0.0-preview.4&prerelease
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 | 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. 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. |
-
net9.0
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 |