Command Architecture
Commands represent write intents — creating, updating, or deleting state. The command side of ArturRios.Mediator enforces a strict…
ArturRios.Mediator is a lightweight .NET library that implements the Mediator pattern on top of the…ArturRios.Mediator is a lightweight .NET library that implements the Mediator pattern on top of the built-in dependency injection container, providing a clean CQRS-style separation between commands (write operations) and queries (read operations).
Each command or query is dispatched to a dedicated handler resolved from a fresh DI scope, so scoped dependencies such as a database context are isolated per execution. Both synchronous and asynchronous variants are supported for every handler type.
dotnet add package ArturRios.Mediator
The package targets net10.0 and depends on ArturRios.Output, which provides the DataOutput<T> and PaginatedOutput<T> result envelopes.
// Program.cs / Startup.cs
builder.Services.AddSingleton<CommandMediator>();
builder.Services.AddSingleton<QueryMediator>();
// or the combined entry point:
builder.Services.AddSingleton<CommandQueryMediator>();
// Register each handler
builder.Services.AddScoped<ICommandHandler<CreateProductCommand, CreateProductOutput>, CreateProductHandler>();
builder.Services.AddScoped<IQueryHandler<GetProductQuery, GetProductOutput>, GetProductHandler>();
public class CreateProductCommand : BaseCommand
{
public string Name { get; set; } = string.Empty;
public decimal Price { get; set; }
}
public class CreateProductOutput : CommandOutput
{
public Guid Id { get; set; }
}
public class CreateProductHandler : ICommandHandlerAsync<CreateProductCommand, CreateProductOutput>
{
public async Task<DataOutput<CreateProductOutput?>> HandleAsync(
CreateProductCommand command,
CancellationToken cancellationToken = default)
{
var id = await _repository.InsertAsync(command.Name, command.Price, cancellationToken);
return DataOutput<CreateProductOutput?>.New.WithData(new CreateProductOutput { Id = id });
}
}
public class ProductsController(CommandQueryMediator mediator) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Create(CreateProductCommand command)
{
var result = await mediator.ExecuteCommandAsync<CreateProductCommand, CreateProductOutput>(
command, HttpContext.RequestAborted);
return result.Success ? Ok(result.Data) : BadRequest(result.Errors);
}
}
The library ships three mediator classes and a full set of handler interfaces:
| Type | Role |
|---|---|
CommandMediator | Dispatches write operations (commands) |
QueryMediator | Dispatches read operations (queries, paginated queries) |
CommandQueryMediator | Unified facade that delegates to both mediators |
For detailed architecture documentation and sequence diagrams see:
Releases that need changes in consuming code carry an upgrade guide in the changelog:
This project is licensed under the Artur Rios Attribution License. It is free and open source: you may view, use, fork, modify, and extend it, provided you credit the author, Artur Rios, with a link to artur-rios.tech or github.com/artur-rios.
Commands represent write intents — creating, updating, or deleting state. The command side of ArturRios.Mediator enforces a strict…
Queries represent read-only requests for data. The query side of ArturRios.Mediator supports two result shapes: a single DataOutput<T> for item lookups…
Notable changes in each release.
Building from source, running the tests, branching and releasing.