Lightweight, high-performance mediator library for .NET. A focused replacement for MediatR, built for Codery projects.
- Request/Response dispatching via
IRequest<TResponse>andIRequestHandler<TRequest, TResponse> - Notifications (multicast) via
INotificationandINotificationHandler<TNotification> - Pipeline behaviors for cross-cutting concerns (logging, validation, etc.)
- Zero per-call reflection — cached generic wrapper pattern eliminates reflection overhead after first use
- Two-package design —
Codery.Mediator.Abstractionshas zero dependencies;Codery.Mediatoradds DI support - Multi-targeting:
net10.0+netstandard2.0
dotnet add package Codery.MediatorRegister in your DI container:
builder.Services.AddCoderyMediator(typeof(Program).Assembly);With pipeline behaviors:
builder.Services.AddCoderyMediator(
opts => opts.AddOpenBehavior(typeof(LoggingBehavior<,>)),
typeof(Program).Assembly);public record GetUser(int Id) : IRequest<UserDto>;
public class GetUserHandler : IRequestHandler<GetUser, UserDto>
{
public Task<UserDto> Handle(GetUser request, CancellationToken cancellationToken)
{
// ...
return Task.FromResult(new UserDto(request.Id, "Alice"));
}
}app.MapGet("/users/{id}", async (int id, ISender sender) =>
await sender.Send(new GetUser(id)));public record OrderPlaced(string OrderId) : INotification;
public class SendEmailHandler : INotificationHandler<OrderPlaced>
{
public Task Handle(OrderPlaced notification, CancellationToken cancellationToken)
{
// Send email...
return Task.CompletedTask;
}
}
// Publish to all handlers
await publisher.Publish(new OrderPlaced("ORD-123"));- Replace
MediatRNuGet withCodery.Mediator - Find & replace:
using MediatR;→using Codery.Mediator; - Replace
services.AddMediatR(cfg => ...)→services.AddCoderyMediator(typeof(Program).Assembly) IRequest(void) handlers useIRequestHandler<TRequest, Unit>(no shorthandIRequestHandler<TRequest>)- Pipeline behaviors:
opts.AddOpenBehavior(typeof(MyBehavior<,>))
| Package | Description |
|---|---|
Codery.Mediator.Abstractions |
Interfaces only. Zero dependencies. Reference from handler projects. |
Codery.Mediator |
Implementation + DI registration. Reference from startup/host project. |