NPv.CQS.Infrastructure 1.1.0

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


✨ Overview

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

It includes:

  • ICommandBuilder and IQueryBuilder for resolving and executing handlers,
  • UnitOfWorkCommandExecutor for transactional command execution,
  • Autofac module for automatic handler registration by namespace.

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


πŸ”§ Key Features

  • πŸ“¦ Built-in ICommandBuilder and IQueryBuilder implementations
  • πŸ“‘ Centralized command and query execution via ICommandBuilder and IQueryBuilder
  • 🧩 CQSModule for seamless Autofac integration via namespace scanning
  • πŸ’Ό UnitOfWorkCommandExecutor for wrapping commands in a Unit of Work with automatic commit
  • πŸ’‘ Compatible with all abstractions from NPv.CQS.Abstractions

πŸ“¦ Installation

dotnet add package NPv.CQS.Infrastructure

πŸ§ͺ Usage Example

1. Register Autofac Module (in Program.cs)

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

builder.Host.ConfigureContainer<ContainerBuilder>(container =>
{
    container.RegisterModule(new CQSModule("YourApp.HandlersNamespace"));
});

2. Execute a Command Declaratively

await _commandBuilder.ExecuteAsync(new CreateOrderCommandContext(orderId, amount));

3. Execute a Query Declaratively

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

No need to resolve handlers manually β€” the infrastructure handles it based on context and criterion types.


4. Execute a Command with Unit of Work

UnitOfWorkCommandExecutor is a new class in the library that wraps command execution in a Unit of Work.

It is automatically registered via CQSModule, so no extra setup is required.
When using it, you no longer inject ICommandBuilder directly into controllers β€” instead, you inject ICommandExecutor.

Class Implementation:

using Autofac;
using NPv.CQS.Abstractions.Commands;
using NPv.Uow.Abstractions;

namespace NPv.CQS.Infrastructure.Commands;

public class UnitOfWorkCommandExecutor(ILifetimeScope root) : ICommandExecutor
{
    public async Task ExecuteAsync<TContext>(TContext ctx) where TContext : ICommandContext
    {
        using var scope = root.BeginLifetimeScope();
        var commands = scope.Resolve<ICommandBuilder>();
        var uow = scope.Resolve<IUnitOfWork>();

        await using (uow)
        {
            await commands.ExecuteAsync(ctx);
            await uow.CommitAsync();
        }
    }
}

Usage in Controller:

[ApiController]
[Route("api/[controller]")]
public class OrdersController(ICommandExecutor executor) : ControllerBase
{
    [HttpPost]
    public Task CreateOrder(CreateOrderCommandContext context)
        => executor.ExecuteAsync(context);
}

🧩 Components Included

Component Purpose
CQSModule Autofac module for scanning and registering command/query handlers
CommandBuilder Resolves and executes ICommand<TContext> for a given context
QueryBuilder Resolves and executes IQuery<TCriterion, TResult> for a given criterion
UnitOfWorkCommandExecutor Executes commands inside a Unit of Work, committing automatically
Default implementations Internal infrastructure for resolving types and linking abstractions

  • NPv.CQS.Abstractions – core interfaces and contracts
  • NPv.Events.Abstractions (planned) – contracts for event-driven extensions

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
1.1.0 137 8/10/2025
1.0.2 121 5/23/2025
1.0.1 150 5/19/2025
1.0.0 144 5/19/2025