IoCTools.Generator
0.4.1
See the version list below for details.
dotnet add package IoCTools.Generator --version 0.4.1
NuGet\Install-Package IoCTools.Generator -Version 0.4.1
<PackageReference Include="IoCTools.Generator" Version="0.4.1" />
<PackageVersion Include="IoCTools.Generator" Version="0.4.1" />
<PackageReference Include="IoCTools.Generator" />
paket add IoCTools.Generator --version 0.4.1
#r "nuget: IoCTools.Generator, 0.4.1"
#:package IoCTools.Generator@0.4.1
#addin nuget:?package=IoCTools.Generator&version=0.4.1
#tool nuget:?package=IoCTools.Generator&version=0.4.1
IoCTools
This repository provides various useful tools for simplifying and streamlining the process of working with Inversion of Control (IoC) in .NET, focusing on ASP.NET services. This project includes a source generator that eliminates much of the boilerplate associated with service registration and dependency injection in .NET.
Features
While this project is in alpha and under active development, many features are already functional.
Self-Describing Services
Services often have specific lifetimes in mind. For example, singleton services persist values during the app session.
IoCTools lets the service itself define its lifetime, removing the need for explicit registration in Program.cs.
Example:
Before (without IoCTools):
// in the service class
namespace SampleProject.Services;
public class SomeService : ISomeService
{
private readonly int _someValue = 1;
}
// in Program.cs
builder.Services.AddSingleton<ISomeService, SomeService>();
After (with IoCTools):
// in the service class
namespace SampleProject.Services;
[Service(Lifetime.Singleton)]
public partial class SomeService : ISomeService
{
private readonly int _someValue = 1;
}
// in Program.cs
builder.Services.RegisterSampleProjectServices();
The service now defines its own lifetime, and registration is streamlined with a single method.
Intuitive Dependency Injection
Dependency injection no longer needs verbose constructors or repetitive field assignments.
Example:
Before (traditional DI):
namespace SampleProject.Services;
public class SomeService(IOtherService otherService, IAnotherService anotherService,
IYetAnotherService yetAnotherService, ISomehowAnotherService somehowAnotherService)
: ISomeService
{
private readonly IOtherService _otherService = otherService;
private readonly IAnotherService _anotherService = anotherService;
private readonly IYetAnotherService _yetAnotherService = yetAnotherService;
private readonly ISomehowAnotherService _somehowAnotherService = somehowAnotherService;
private readonly int _someValue = 1;
}
After (with IoCTools):
namespace SampleProject.Services;
[Service]
public partial class SomeService : ISomeService
{
[Inject] private readonly IOtherService _otherService;
[Inject] private readonly IAnotherService _anotherService;
[Inject] private readonly IYetAnotherService _yetAnotherService;
[Inject] private readonly ISomehowAnotherService _somehowAnotherService;
private readonly int _someValue = 1;
}
IoCTools generates the constructor, simplifying DI management. It supports dependencies of any generic depth, such as
IEnumerable<T>.
Streamlined Service Registration
IoCTools eliminates repetitive service registration by generating an extension method to register all [Service]
-annotated classes automatically.
Before:
builder.Services.AddSingleton<ISomeService, SomeService>();
builder.Services.AddScoped<IAnotherService, AnotherService>();
builder.Services.AddTransient<IOtherService, OtherService>();
After:
builder.Services.RegisterSampleProjectServices();
This method is project-specific, avoiding naming conflicts when multiple projects use IoCTools.
Generic Service Support
IoCTools fully supports the registration of generic services.
Example:
namespace SampleProject.Services;
[Service]
public class GenericService<T> : IGenericService<T>
{
}
Generated Code:
services.AddScoped(typeof(IGenericService<>), typeof(GenericService<>));
Excluding Services from Registration
For cases where a service shouldn’t be registered (e.g., controllers or handlers), use [UnregisteredService] to
exclude it explicitly.
namespace SampleProject.Controllers;
[UnregisteredService]
public class MyController : ControllerBase
{
}
DependsOn: Declarative Dependency Injection
DependsOn offers a concise way to define dependencies with default naming conventions.
Example:
namespace SampleProject.Services;
[Service]
[DependsOn<IOtherService, IAnotherService, IYetAnotherService, ISomehowAnotherService>]
public partial class SomeService : ISomeService
{
private readonly int _someValue = 1;
}
IoCTools generates fields for these dependencies, allowing the service to use them directly.
You can customize field naming with the following options:
namingConvention: UseCamelCase(default),PascalCase, orSnakeCase.stripI: Removes the leadingIin interface names (default:true).prefix: Adds a prefix to field names (default:_).
Example with Customization:
[DependsOn<IOtherService, IAnotherService>(namingConvention: NamingConvention.PascalCase, prefix: "field_")]
[DependsOn<IYetAnotherService, ISomehowAnotherService>]
public partial class SomeService : ISomeService
{
}
Development Status
IoCTools is in active development, with public pre-release NuGet packages available. Expect breaking changes as the project evolves.
Feel free to contribute or report issues!
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.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.9.1 | 97 | 5/23/2026 |
| 1.8.0 | 3,918 | 5/12/2026 |
| 1.7.3 | 1,578 | 5/6/2026 |
| 1.7.2 | 97 | 5/6/2026 |
| 1.7.1 | 91 | 5/6/2026 |
| 1.6.1 | 99 | 4/29/2026 |
| 1.6.0 | 99 | 4/29/2026 |
| 1.5.1 | 142 | 4/12/2026 |
| 1.4.0 | 213 | 3/21/2026 |
| 1.3.0 | 119 | 1/24/2026 |
| 1.2.0 | 408 | 11/18/2025 |
| 1.1.0 | 293 | 11/12/2025 |
| 1.0.0 | 199 | 9/10/2025 |
| 1.0.0-alpha | 231 | 8/28/2025 |
| 0.4.1 | 160 | 11/30/2024 |
| 0.4.0 | 150 | 11/30/2024 |
| 0.3.0 | 226 | 3/18/2024 |
| 0.2.6 | 260 | 2/6/2024 |
| 0.2.5 | 262 | 2/6/2024 |
| 0.2.4 | 267 | 2/6/2024 |