A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling, persistence, unit of work, mediator, distributed messaging, event bus, CQRS, email, and more
$ dotnet add package RCommon.ApplicationServicesProvides a CQRS (Command Query Responsibility Segregation) implementation with dedicated command and query buses, handler registration, and optional validation integration for the RCommon framework.
ICommandHandler<TResult, TCommand> and returns an IExecutionResultIQueryHandler<TQuery, TResult> and returns a typed resultIValidationServiceAddRCommon() builder pattern for clean DI configurationdotnet add package RCommon.ApplicationServices
using RCommon;
using RCommon.ApplicationServices;
// Configure CQRS in your DI setup
services.AddRCommon(config =>
{
config.WithCQRS<CqrsBuilder>(cqrs =>
{
// Register handlers individually
cqrs.AddCommandHandler<CreateOrderHandler, CreateOrderCommand, CommandResult>();
cqrs.AddQueryHandler<GetOrderHandler, GetOrderQuery, OrderDto>();
// Or scan an assembly for all handlers
cqrs.AddCommandHandlers(typeof(CreateOrderHandler).Assembly);
cqrs.AddQueryHandlers(typeof(GetOrderHandler).Assembly);
});
});
// Dispatch a command from your application layer
public class OrderService
{
private readonly ICommandBus _commandBus;
private readonly IQueryBus _queryBus;
public OrderService(ICommandBus commandBus, IQueryBus queryBus)
{
_commandBus = commandBus;
_queryBus = queryBus;
}
public async Task<CommandResult> CreateOrderAsync(CreateOrderCommand command)
{
return await _commandBus.DispatchCommandAsync(command);
}
public async Task<OrderDto> GetOrderAsync(GetOrderQuery query)
{
return await _queryBus.DispatchQueryAsync(query);
}
}
services.AddRCommon(config =>
{
config.WithValidation<FluentValidationBuilder>(validation =>
{
validation.UseWithCqrs(options =>
{
options.ValidateCommands = true;
options.ValidateQueries = true;
});
});
});| Type | Description |
|---|---|
ICommandBus | Dispatches commands to their registered handler and returns an IExecutionResult |
IQueryBus | Dispatches queries to their registered handler and returns a typed result |
ICommandHandler<TResult, TCommand> | Handles a specific command type and produces an execution result |
IQueryHandler<TQuery, TResult> | Handles a specific query type and produces a result |
IValidationService | Validates objects before dispatch; integrates with the CQRS pipeline |
ValidationOutcome | Contains a list of ValidationFault errors produced by validation |
ValidationFault | Describes a single validation failure with property name, message, and severity |
CqrsValidationOptions | Controls whether commands and/or queries are validated before dispatch |
CqrsBuilder | Default ICqrsBuilder implementation that registers CommandBus and QueryBus |
For full documentation, visit rcommon.com.
IValidationProvider for CQRS pipeline integrationICommand, IQuery, and IExecutionResult model contractsLicensed under the Apache License, Version 2.0.