ModulusKit.Mediator
1.1.0
dotnet add package ModulusKit.Mediator --version 1.1.0
NuGet\Install-Package ModulusKit.Mediator -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="ModulusKit.Mediator" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ModulusKit.Mediator" Version="1.1.0" />
<PackageReference Include="ModulusKit.Mediator" />
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 ModulusKit.Mediator --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ModulusKit.Mediator, 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 ModulusKit.Mediator@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=ModulusKit.Mediator&version=1.1.0
#tool nuget:?package=ModulusKit.Mediator&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Modulus.Mediator
Lightweight CQRS mediator for .NET with pipeline behaviors, validation, logging, and a built-in Result pattern.
Installation
dotnet add package ModulusKit.Mediator
Setup
services.AddModulusMediator(typeof(Program).Assembly);
// Add built-in pipeline behaviors
services.AddPipelineBehavior(typeof(UnhandledExceptionBehavior<,>));
services.AddPipelineBehavior(typeof(LoggingBehavior<,>));
services.AddPipelineBehavior(typeof(ValidationBehavior<,>));
AddModulusMediator scans the provided assemblies and auto-registers all handlers (ICommandHandler<>, IQueryHandler<,>, IStreamQueryHandler<,>, IDomainEventHandler<>).
Usage
Define a command and handler
public record CreateOrder(string CustomerId, List<OrderItem> Items) : ICommand<Guid>;
public class CreateOrderHandler : ICommandHandler<CreateOrder, Guid>
{
public async Task<Result<Guid>> Handle(CreateOrder command, CancellationToken ct)
{
var order = Order.Create(command.CustomerId, command.Items);
await _repository.Add(order, ct);
return Result<Guid>.Success(order.Id);
}
}
Define a query and handler
public record GetOrderById(Guid Id) : IQuery<OrderDto>;
public class GetOrderByIdHandler : IQueryHandler<GetOrderById, OrderDto>
{
public async Task<Result<OrderDto>> Handle(GetOrderById query, CancellationToken ct)
{
var order = await _repository.GetById(query.Id, ct);
if (order is null)
return Error.NotFound("Order.NotFound", "Order was not found");
return Result<OrderDto>.Success(order.ToDto());
}
}
Send commands and queries
var result = await mediator.Send(new CreateOrder("cust-1", items));
if (result.IsSuccess)
Console.WriteLine($"Created order: {result.Value}");
else
Console.WriteLine($"Failed: {result.Errors[0].Description}");
Pipeline Behaviors
Behaviors wrap every request in a middleware-style pipeline. They execute in registration order (first registered = outermost):
| Behavior | Purpose |
|---|---|
UnhandledExceptionBehavior |
Catches unhandled exceptions and converts them to failure Results |
LoggingBehavior |
Logs request start, elapsed time, and success/failure |
ValidationBehavior |
Runs FluentValidation validators and short-circuits on errors |
Custom behaviors
public class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : notnull
{
private readonly IUnitOfWork _unitOfWork;
public UnitOfWorkBehavior(IUnitOfWork unitOfWork) => _unitOfWork = unitOfWork;
public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
var response = await next();
// Commit only on success (TResponse is always Result or Result<T>)
if (response is Result { IsSuccess: true })
await _unitOfWork.CommitAsync(cancellationToken);
return response;
}
}
Register custom behaviors:
services.AddPipelineBehavior(typeof(UnitOfWorkBehavior<,>));
Domain Events
public record OrderPlaced(Guid OrderId, string CustomerId) : IDomainEvent;
public class OrderPlacedHandler : IDomainEventHandler<OrderPlaced>
{
public async Task Handle(OrderPlaced domainEvent, CancellationToken ct)
{
// React to the event
}
}
// Publish
await mediator.Publish(new OrderPlaced(order.Id, order.CustomerId));
Learn More
See the Modulus repository for full documentation.
| 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- FluentValidation (>= 12.1.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Diagnostics.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.3)
- ModulusKit.Mediator.Abstractions (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.