NPv.CQS.Infrastructure
3.0.6
dotnet add package NPv.CQS.Infrastructure --version 3.0.6
NuGet\Install-Package NPv.CQS.Infrastructure -Version 3.0.6
<PackageReference Include="NPv.CQS.Infrastructure" Version="3.0.6" />
<PackageVersion Include="NPv.CQS.Infrastructure" Version="3.0.6" />
<PackageReference Include="NPv.CQS.Infrastructure" />
paket add NPv.CQS.Infrastructure --version 3.0.6
#r "nuget: NPv.CQS.Infrastructure, 3.0.6"
#:package NPv.CQS.Infrastructure@3.0.6
#addin nuget:?package=NPv.CQS.Infrastructure&version=3.0.6
#tool nuget:?package=NPv.CQS.Infrastructure&version=3.0.6
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.
π Breaking changes
v3.0.0
- Target framework updated to
net10.0(droppednet9.0support).
This is a personal library that focuses on the latest .NET runtime to keep maintenance simple and enjoyable.
β¨ Overview
NPv.CQS.Infrastructure provides essential building blocks for executing commands and queries defined using NPv.CQS.Abstractions.
It includes:
ICommandBuilderandIQueryFor<TResult>for resolving and executing handlers,UnitOfWorkCommandExecutorfor command execution with commit on success,- 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, CancellationToken ct = default) { 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
IServiceCollectionextensions. - Handler registration includes fail-fast duplicate detection by full generic service contract.
π§ Key Features
- π¦ Built-in
ICommandBuilderandIQueryFor<TResult>implementations - π‘ Centralized command and query execution
- πΌ
UnitOfWorkCommandExecutorfor 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));
Execution and validation boundary
These semantics apply to the implementations registered by AddCqsInfrastructure:
ICommandExecutor(UnitOfWorkCommandExecutor) creates and disposes a separate async DI scope for every root execution, resolvingICommandBuilderandIUnitOfWorkthere. It commits that UoW only when the handler returns a successfulResultorResult<TResult>; failure skips commit.ICommandBuilderdispatches the handler in its current DI scope and does not commit. Nested builder calls share the current scope/UoW; the root executor owns commit based on the root result. Propagate nested failures when they must prevent that commit.- A nested executor call creates a separate scope/UoW and execution/commit boundary. Its commit is independent of the outer result; nested executor calls do not form one atomic operation.
- CQS does not run DataAnnotations validation. A command callable outside HTTP must enforce required application-level validation and invariants itself (in the handler or application/domain logic it invokes); HTTP validation alone is insufficient.
- The execution scope does not automatically put handler reads or external side effects before commit inside a database transaction. See UoW transaction limits.
Use the executor to own a root command's scope and commit; use the builder to compose commands within an existing scope/UoW. Outside an executor, the caller owns commit.
Handler registration
AddCqsInfrastructure scans the supplied assemblies. Handler identity is the full closed generic service contract: ICommand<TContext>, ICommand<TContext, TResult>, or IQuery<TCriterion, TResult>, including every generic argument. Registering one does not satisfy another contract with a different result type or generic arity.
For each dispatch, check that the handler's assembly is included and its implemented interface matches the exact context/criterion and result types used by that call. Duplicate detection rejects an already registered service contract during scanning; it does not prove that all required handlers are registered. A missing registration fails at dispatch.
π§© 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 |
π Related Packages
NPv.CQS.Abstractionsβ core interfaces and contractsNPv.ResultPatternβ lightweight Result pattern implementationNPv.Uow.Abstractionsβ unit of work contract for transactional execution
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- NPv.CQS.Abstractions (>= 3.0.0)
- NPv.ResultPattern (>= 2.0.0)
- NPv.Uow.Abstractions (>= 2.0.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 |
|---|---|---|
| 3.0.6 | 30 | 10/25/2026 |
| 3.0.5 | 145 | 9/9/2026 |
| 3.0.4 | 138 | 8/17/2026 |
| 3.0.3 | 154 | 7/2/2026 |
| 3.0.2 | 169 | 3/7/2026 |
| 3.0.1 | 173 | 2/1/2026 |
| 3.0.0 | 242 | 12/24/2025 |
| 2.4.1 | 262 | 10/1/2025 |
| 2.4.0 | 247 | 10/1/2025 |
| 2.3.1 | 175 | 9/27/2025 |
| 2.3.0 | 183 | 9/27/2025 |
| 2.2.0 | 195 | 9/26/2025 |
| 2.1.1 | 276 | 9/22/2025 |
| 2.1.0 | 267 | 9/22/2025 |
| 2.0.0 | 268 | 9/21/2025 |
| 1.1.0 | 266 | 8/10/2025 |
| 1.0.2 | 217 | 5/23/2025 |
| 1.0.1 | 236 | 5/19/2025 |
| 1.0.0 | 242 | 5/19/2025 |