Fluens.Cqrs
0.9.4
dotnet add package Fluens.Cqrs --version 0.9.4
NuGet\Install-Package Fluens.Cqrs -Version 0.9.4
<PackageReference Include="Fluens.Cqrs" Version="0.9.4" />
<PackageVersion Include="Fluens.Cqrs" Version="0.9.4" />
<PackageReference Include="Fluens.Cqrs" />
paket add Fluens.Cqrs --version 0.9.4
#r "nuget: Fluens.Cqrs, 0.9.4"
#:package Fluens.Cqrs@0.9.4
#addin nuget:?package=Fluens.Cqrs&version=0.9.4
#tool nuget:?package=Fluens.Cqrs&version=0.9.4
Fluens.Cqrs
CQRS pattern implementation with command/query dispatchers, handler interfaces, and logging/tracing decorators.
Installation
dotnet add package Fluens.Cqrs
Usage
Registration
fluensBuilder
.AddCqrs()
.AddCqrsHandlers<Program>()
.AddCqrsLoggingDecorator()
.AddCqrsTracingDecorator();
AddCqrsLoggingDecorator() and AddCqrsTracingDecorator() may be called in any order relative to
AddCqrsHandlers<T>() / AddHandlers() — the decorator request is parked and applied to every current
and future matching handler registration. Every command handler is wrapped by each requested decorator
exactly once, however often and in whatever order AddCqrsHandlers<T>(), AddHandlers(),
AddCqrsLoggingDecorator() and AddCqrsTracingDecorator() are called (each call is a registration pass,
and a pass never adds a second layer); the decorator requested later wraps outermost, and a handler
registered by a later pass gets both decorators in the same order.
The container owns the decorated handler. The first decoration of a handler moves its original registration
to a keyed registration of the same service type and lifetime under a private key, and the decorator resolves
the inner handler from it. A decorated handler is therefore created, validated and disposed exactly as without
decorators: a scoped handler that implements IDisposable or IAsyncDisposable is disposed with its scope, a
handler registered through an implementation factory is disposed by the container, and a handler registered as
an implementation instance is never disposed by it. Because the inner handler is registered in the container,
ValidateOnBuild checks its constructor dependencies as it does for an undecorated handler.
The tracing decorator emits spans on the Fluens.Cqrs
ActivitySource (owned statically by the library, never injected via DI); subscribe to it by listing
"Fluens.Cqrs" in ObservabilityOptions.AdditionalSources.
Commands
// Command without return value
public record CreateOrder(string Number) : ICommand;
public class CreateOrderHandler : ICommandHandler<CreateOrder>
{
public async Task<Result> HandleAsync(CreateOrder command, CancellationToken cancellationToken)
{
// ...
return Result.Success();
}
}
// Command with return value
public record GetOrder(int Id) : ICommand<OrderDto>;
public class GetOrderHandler : ICommandHandler<GetOrder, OrderDto>
{
public async Task<Result<OrderDto>> HandleAsync(GetOrder command, CancellationToken cancellationToken)
{
// ...
return Result.Success(new OrderDto());
}
}
ICommand<TResult> is invariant in TResult, like IQuery<TResult>: a command can only be dispatched under its declared result type (GetOrder as ICommand<OrderDto>). Passing it as ICommand<object> does not compile.
Paginated Queries
public class SearchOrders : PagedQuery<PagedResult<OrderDto>>
{
public string? Term { get; set; }
}
PagedQuery<TResult> is an abstract class (not a record — a record cannot derive from a plain
abstract class) that provides default pagination properties with [JsonPropertyName] short codes to
minimize JSON payload:
| Property | Short code | Default |
|---|---|---|
Page |
p |
1 |
PageSize |
ps |
20 |
SortOrder |
so |
null |
SortProperty |
sp |
null |
Page and PageSize are clamped on assignment via PagingDefaults.ClampPage /
ClampPageSize: null passes through unclamped (meaning "not specified"), Page is floored at
DefaultPage (the page number is 1-based), and PageSize is clamped between MinPageSize and
MaxPageSize. Subclasses automatically inherit the short names in any JsonSerializerContext - no extra
configuration required. Pagination constants are available via Constants.Cqrs.Query
(DefaultPage, DefaultPageSize, MinPageSize, MaxPageSize).
Queries
public record ListOrders(string? Term) : IQuery<IReadOnlyList<OrderDto>>;
public class ListOrdersHandler : IQueryHandler<ListOrders, IReadOnlyList<OrderDto>>
{
public async Task<IReadOnlyList<OrderDto>> HandleAsync(ListOrders query, CancellationToken cancellationToken)
{
// Queries return the result directly (no Result wrapper)
return [];
}
}
Module-Level Registration
// In IModuleSetup.RegisterServices:
module.AddHandlers(); // scans module assembly, validates assembly boundaries
AddHandlers() scans the module assembly for handler implementations, registers them with scoped lifetime, and enforces module boundary rules: handlers can only handle commands/queries defined in the same module assembly, and cross-module dispatch at runtime throws InvalidOperationException. The runtime guard runs on all three ICommandDispatcher.SendAsync overloads (including the strongly-typed SendAsync<TCommand, TResult>) and on IQueryDispatcher.SendAsync. Only AddHandlers() registers an assembly for the boundary check. Command and query types from assemblies registered only by AddCqrsHandlers<T>() are never checked.
Dispatching
// Inject ICommandDispatcher / IQueryDispatcher
Result result = await commandDispatcher.SendAsync(new CreateOrder("ORD-001"), ct);
IReadOnlyList<OrderDto> orders = await queryDispatcher.SendAsync(new ListOrders("test"), ct);
Built-in decorators: LoggingCommandHandlerDecorator (logs execution), TracingCommandHandlerDecorator
(OpenTelemetry activity on the Fluens.Cqrs source — sets ActivityStatusCode.Ok on success and
ActivityStatusCode.Error both when the inner handler throws and when it returns a failed Result).
When the handler throws while its CancellationToken is cancelled (e.g. the client aborted the HTTP
request and SqlClient threw SqlException), the span gets a cancelled event instead and its status
stays unset - a caller cancellation is not a failure. The exception is always rethrown.
License
This project is licensed under the MIT License.
| 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
- Fluens (>= 0.9.4)
- Fluens.Kernel (>= 0.9.4)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.13)
- Scrutor (>= 7.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 |
|---|---|---|
| 0.9.4 | 252 | 11/30/2026 |
| 0.9.3 | 281 | 11/17/2026 |
| 0.9.2 | 131 | 10/21/2026 |
| 0.9.1 | 471 | 10/13/2026 |
| 0.9.0 | 108 | 10/13/2026 |
| 0.8.7 | 261 | 10/12/2026 |
| 0.8.6 | 120 | 10/12/2026 |
| 0.8.5 | 118 | 10/7/2026 |
| 0.8.4 | 106 | 10/6/2026 |
| 0.8.3 | 109 | 10/6/2026 |
| 0.8.2 | 179 | 10/5/2026 |
| 0.8.1 | 154 | 9/15/2026 |
| 0.8.0 | 117 | 9/15/2026 |
| 0.7.11 | 122 | 9/15/2026 |
| 0.7.10 | 126 | 9/13/2026 |
| 0.7.9 | 125 | 9/11/2026 |
| 0.7.8 | 134 | 9/10/2026 |
| 0.7.7 | 143 | 9/10/2026 |
| 0.7.6 | 185 | 7/1/2026 |
| 0.7.5 | 148 | 6/22/2026 |