Skip to content

Latest commit

 

History

309 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo DependencyModules

NuGet build coverage License: MIT

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

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:

  • SutProject and SecondarySutProject: modules and services.
  • SutProject.Tests: xUnit tests of these modules.
  • SutProject.NUnitTests: NUnit tests of these modules.
  • ConsoleTestProject: a console application.
  • web/WebApiApp and web/WebApiApp.Tests: an ASP.NET Core application and its tests.

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, with xunit.v3 version 3.
DependencyModules.xUnit4 [ModuleTest] for xUnit v3, with xunit.v3 version 4.
DependencyModules.NUnit [ModuleTest] and [ModuleTestCase] for NUnit 4.
DependencyModules.Testing The test attributes and interfaces that the xUnit and NUnit packages use. It also gives the test that runs, and a logger that writes to the output of the test.
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.

License

DependencyModules has the MIT license.

About

Attribute-driven dependency injection modules for .NET, generated at compile time. No reflection, no assembly scanning.

Topics

Resources

Contributing

Stars

13 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages