Kuna.Extensions.DependencyInjection.Validation 1.0.1

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

Kuna.Extensions.DependencyInjection.Validation

Effortless testing of dependency registrations with Microsoft.Extensions.DependencyInjection

Installation

Install the Nuget Package

Package Manager Console

Install-Package Kuna.Extensions.DependencyInjection.Validation

.NET Core CLI

dotnet add package Kuna.Extensions.DependencyInjection.Validation

Usage

This library allows you to scan assemblies and verify that all dependencies required for the instantiation of types are registered with the dependency injection container. It has been tested exclusively with the base container implementation provided by Microsoft.

To utilize the library, you may add a test to your API project using the structure provided in the example.

First, create an instance of the generic RegistrationValidator, where the generic type parameter is the entry point for your application (e.g. Program). Then, call the Validate method and provide the types to be tested. The RegistrationValidator will scan the assemblies for definitions of the specified types, including concrete types and open generic interface definitions. For example, if you are using MediatR to dispatch commands or events, you may specify open generic interfaces such as IRequest<> or INotificationHandler<>.

The WebApplicationFactory is used to initiate the bootstrapping process for the application, which configures the ServiceCollection. To prevent the app from running, it is necessary to handle two exceptions: FailureException and SuccessException. When the SuccessException is thrown, it can be safely ignored, and optional feedback can be provided by calling Result.ToString() and writing it to the output console. In the event of a FailureException, the test will fail when you call Assert.Failure (which works for both XUnit and NUnit). You can then call Result.ToString() to display the failures. If you desire a different output format, you may create an extension for the Result type.

Example

public class ServicesConfigurationTest
{
    private readonly ITestOutputHelper console;

    public ServicesConfigurationTest(ITestOutputHelper console)
    {
        this.console = console;
    }

    [Fact]
    public void VerifyDependencyRegistrations()
    {
        var assemblies = new[]
        {
            typeof(ShoppingCartsController).Assembly, // api
            typeof(IShoppingCartRepository).Assembly, // application
        };

        var verifier = new RegistrationValidator<Program>(assemblies);

        try
        {
            verifier.Validate(
                typeof(Controller),
                typeof(IHandleCommand<>));
        }
        catch (FailureException fe)
        {
            this.console.WriteLine(fe.Result.ToString());

            Assert.Fail("Could not resolve all dependencies");
        }
        catch (SuccessException e)
        {
            this.console.WriteLine(e.Result.ToString());
        }
    }
}
Example of output
Xunit.Sdk.FailException
Assert.Fail(): Could not resolve all dependencies
   at Carts.Api.Tests.ServicesConfigurationTest.VerifyDependencyRegistrations() in C:\Github\Kuna\Kuna.EventSourcing\Sample\ECommerce\Carts\Carts.Api.Tests\ServicesConfigurationTests.cs:line 43



Number of types to verify: 6

Failure messages:
-----------------------------------------
Unable to resolve service for type 'Carts.Domain.Services.IProductPriceCalculator' while attempting to activate 'Carts.Application.CommandHandlers.AddProductHandler'.

Failed to resolve:
-----------------------------------------
Kuna.EventSourcing.Core.Commands.IHandleCommand`1[[Carts.Domain.Commands.AddProduct, Carts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Resolved successfuly:
-----------------------------------------
Carts.Api.Controllers.ShoppingCartsController
Kuna.EventSourcing.Core.Commands.IHandleCommand`1[[Carts.Domain.Commands.CancelShoppingCart, Carts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Kuna.EventSourcing.Core.Commands.IHandleCommand`1[[Carts.Domain.Commands.ConfirmShoppingCart, Carts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Kuna.EventSourcing.Core.Commands.IHandleCommand`1[[Carts.Domain.Commands.OpenShoppingCart, Carts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Kuna.EventSourcing.Core.Commands.IHandleCommand`1[[Carts.Domain.Commands.RemoveProduct, Carts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.1 412 1/2/2023