DependencyModules.SourceGenerator
1.6.1
dotnet add package DependencyModules.SourceGenerator --version 1.6.1
NuGet\Install-Package DependencyModules.SourceGenerator -Version 1.6.1
<PackageReference Include="DependencyModules.SourceGenerator" Version="1.6.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="DependencyModules.SourceGenerator" Version="1.6.1" />
<PackageReference Include="DependencyModules.SourceGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DependencyModules.SourceGenerator --version 1.6.1
#r "nuget: DependencyModules.SourceGenerator, 1.6.1"
#:package DependencyModules.SourceGenerator@1.6.1
#addin nuget:?package=DependencyModules.SourceGenerator&version=1.6.1
#tool nuget:?package=DependencyModules.SourceGenerator&version=1.6.1
DependencyModules
DependencyModules is a source generator for Microsoft.Extensions.DependencyInjection. You put attributes on your classes. When you compile the project, the generator writes the code that registers these classes in an IServiceCollection. The generated code does not use reflection to find services at run time.
A module is a partial class that registers the services of a project. A module can use other modules. The test packages build a service provider from your modules for each xUnit or NUnit test.
Documentation: ipjohnson.github.io/DependencyModules
Packages
| Package | Contents |
|---|---|
| DependencyModules.Runtime | The attributes, the interfaces, and the AddModule methods. |
| DependencyModules.SourceGenerator | The source generator. It operates only when you compile. |
| DependencyModules.xUnit | [ModuleTest] for xUnit v3. |
| DependencyModules.NUnit | [ModuleTest] and [ModuleTestCase] for NUnit 4. |
| DependencyModules.Testing | The test attributes and interfaces that the xUnit and NUnit packages use. |
| DependencyModules.NSubstitute | [Mock] parameters with NSubstitute. |
| DependencyModules.Moq | [Mock] and Mock<T> parameters with Moq. |
| DependencyModules.FakeItEasy | [Mock] parameters with FakeItEasy. |
| DependencyModules.SourceGenerator.Impl | The source code of the generator. A framework can compile this code into a different source generator. |
The target frameworks of the runtime package, DependencyModules.Testing, the test packages, and the mock packages are net8.0 and net10.0. The generator is compatible with Roslyn 4.10 and all subsequent versions.
Install
dotnet add package DependencyModules.Runtime
dotnet add package DependencyModules.SourceGenerator
Register services
Put a service attribute on each class. The attribute sets the lifetime.
using DependencyModules.Runtime.Attributes;
namespace Shop;
public interface IPriceCalculator
{
decimal Total(decimal price, int quantity);
}
[SingletonService]
public class PriceCalculator : IPriceCalculator
{
public decimal Total(decimal price, int quantity) => price * quantity;
}
[DependencyModule]
public partial class ShopModule;
The generator registers PriceCalculator as IPriceCalculator. ShopModule registers all services of the project.
Load the module
using DependencyModules.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Shop;
var services = new ServiceCollection();
services.AddModule<ShopModule>();
var provider = services.BuildServiceProvider();
var calculator = provider.GetRequiredService<IPriceCalculator>();
Tests with modules
using DependencyModules.xUnit.Attributes;
using Shop;
using Xunit;
namespace Shop.Tests;
public class PriceCalculatorTests
{
[ModuleTest(typeof(ShopModule))]
public void Multiplies(IPriceCalculator calculator)
{
Assert.Equal(10m, calculator.Total(2.5m, 4));
}
}
The test gets its parameters from a new service provider. After the test, the test package disposes the service provider.
Features
- Services: lifetimes, keyed services, registration types, cross-wired services, and factory methods.
- Modules: module dependencies, realms, module parameters, and the generated
ApplicationModule. - Conventions: registrations for all classes that implement an interface.
- Decorators and interception: code around services.
- Environments: registrations that occur only in some environments.
- Testing: xUnit and NUnit tests with modules, mocks, and more service providers in a test.
- Native AOT: generated factories and trimming.
Sample projects
The integ-tests folder contains projects that reference the source projects of the solution. The solution builds them, and the build workflow runs their tests:
SutProjectandSecondarySutProject: modules and services.SutProject.Tests: xUnit tests of these modules.SutProject.NUnitTests: NUnit tests of these modules.ConsoleTestProject: a console application.web/WebApiAppandweb/WebApiApp.Tests: an ASP.NET Core application and its tests.
License
DependencyModules has the MIT license.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (11)
Showing the top 5 NuGet packages that depend on DependencyModules.SourceGenerator:
| Package | Downloads |
|---|---|
|
SimpleRequest.Aws.Host.Runtime
Package Description |
|
|
SimpleRequest.Aws.Host.DdbStream
Package Description |
|
|
SimpleRequest.Aws.Host.Sqs
Package Description |
|
|
SimpleRequest.Aws.Lambda.CwDashboard
Package Description |
|
|
SimpleRequest.Aws.Lambda.Runtime
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.6.1 | 0 | 9/24/2026 |
| 1.6.0 | 0 | 9/24/2026 |
| 1.5.0 | 566 | 9/9/2026 |
| 1.4.0 | 103 | 9/8/2026 |
| 1.3.1 | 406 | 9/5/2026 |
| 1.3.0 | 318 | 9/1/2026 |
| 1.2.3 | 105 | 8/31/2026 |
| 1.2.3-preview.1 | 73 | 8/29/2026 |
| 1.2.2 | 230 | 8/29/2026 |
| 1.2.1 | 100 | 8/28/2026 |
| 1.2.0 | 172 | 8/28/2026 |
| 1.1.0 | 105 | 8/26/2026 |
| 1.0.0 | 309 | 8/16/2026 |
| 1.0.0-rc9340 | 104 | 8/16/2026 |
| 1.0.0-rc9230 | 610 | 8/12/2026 |
| 1.0.0-rc9220 | 127 | 8/11/2026 |
| 1.0.0-rc9210 | 94 | 8/9/2026 |
| 1.0.0-rc9200 | 143 | 8/7/2026 |
| 1.0.0-RC9177 | 746 | 3/12/2026 |
| 1.0.0-RC9166 | 191 | 3/7/2026 |