NPv.CQS.Infrastructure 2.4.1

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

NPv.CQS.Infrastructure

Infrastructure for declarative execution of commands and queries in CQS-based applications.
Includes default implementations, Unit of Work executor, and Microsoft.Extensions.DependencyInjection integration.


✨ Overview

NPv.CQS.Infrastructure provides essential building blocks for executing commands and queries defined using NPv.CQS.Abstractions.

It includes:

  • ICommandBuilder and IQueryFor<TResult> for resolving and executing handlers,
  • UnitOfWorkCommandExecutor for transactional command execution,
  • Service collection extensions for automatic handler registration with fail-fast duplicate detection.

This package is intended for the application or composition root layers, where behaviors are wired and executed.


🚨 Breaking Changes in v2.0.0

Starting with v2.1.0, all commands now return a Result (from NPv.ResultPattern) instead of being void tasks.

  • Before (v1.x):

    public class CreateOrderCommand : ICommand<CreateOrderCommandContext>
    {
        public Task ExecuteAsync(CreateOrderCommandContext context)
        {
            // Domain logic
            return Task.CompletedTask;
        }
    }
    
  • After (v2.x):

    public class CreateOrderCommand : ICommand<CreateOrderCommandContext>
    {
        public Task<Result> ExecuteAsync(CreateOrderCommandContext context)
        {
            if (context.Amount <= 0)
                return Task.FromResult(Result.Failure(new Error("Validation.Invalid", "Amount")));
    
            // Domain logic
            return Task.FromResult(Result.Success());
        }
    }
    

ICommandBuilder and ICommandExecutor have been updated to propagate the Result.
UnitOfWorkCommandExecutor now commits the Unit of Work only if the result was successful.


🚨 Breaking Changes in v2.4.0

As of v2.4.0, Autofac support has been completely removed.
The package now relies solely on Microsoft.Extensions.DependencyInjection (MS.DI).

  • CQSModule (Autofac integration) has been removed.
  • Registration is now done through IServiceCollection extensions.
  • All handler registrations include fail-fast duplicate detection (an exception is thrown if multiple handlers for the same context/criterion are found).

πŸ”§ Key Features

  • πŸ“¦ Built-in ICommandBuilder and IQueryFor<TResult> implementations
  • πŸ“‘ Centralized command and query execution
  • πŸ’Ό UnitOfWorkCommandExecutor for wrapping commands in a Unit of Work with automatic commit on success
  • πŸ” Fail-fast duplicate detection for handler registration
  • πŸ’‘ Compatible with all abstractions from NPv.CQS.Abstractions

πŸ“¦ Installation

dotnet add package NPv.CQS.Infrastructure

πŸ§ͺ Usage Example

1. Register Infrastructure (in Program.cs)

using NPv.CQS.Infrastructure;

builder.Services.AddCqsInfrastructure(new[]
{
    typeof(CreateOrderCommand).Assembly,
    typeof(FindByIdQuery).Assembly
});

2. Execute a Command Declaratively

var result = await _commandExecutor.ExecuteAsync(new CreateOrderCommandContext(orderId, amount));
if (result.IsFailure)
{
    return BadRequest(result.Errors);
}
return Ok();

3. Execute a Query Declaratively

var order = await _queryFor<Order>()
    .With(new FindById(orderId));

🧩 Components Included

Component Purpose
CommandBuilder Resolves and executes ICommand<TContext> for a given context
QueryFor<TResult> Resolves and executes IQuery<TCriterion, TResult> for a given criterion
UnitOfWorkCommandExecutor Executes commands inside a Unit of Work, committing only on success
ServiceCollectionExtensions Registers commands and queries with duplicate handler detection


Author's Note

This library grew out of my long-standing personal interest in structuring and publishing open source packages. Over time, I’ve revisited and refined earlier internal utilities and ideas, giving them a more consistent shape and preparing them for wider reuse. Along the way, I’ve also taken the opportunity to explore how open source distribution and licensing work in the .NET ecosystem.

It’s a small step toward something I’ve always wanted to try β€” sharing practical, minimal tools that reflect years of learning, experimentation, and refinement.

Hopefully, someone finds it useful.

Nikolai πŸ˜›

βš–οΈ License

MIT β€” you are free to use this in commercial and open-source software.

Product 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. 
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
2.4.1 43 10/1/2025
2.4.0 37 10/1/2025
2.3.1 68 9/27/2025
2.3.0 67 9/27/2025
2.2.0 88 9/26/2025
2.1.1 175 9/22/2025
2.1.0 174 9/22/2025
2.0.0 191 9/21/2025
1.1.0 159 8/10/2025
1.0.2 135 5/23/2025
1.0.1 161 5/19/2025
1.0.0 155 5/19/2025