Dom.Mediator
1.0.4
dotnet add package Dom.Mediator --version 1.0.4
NuGet\Install-Package Dom.Mediator -Version 1.0.4
<PackageReference Include="Dom.Mediator" Version="1.0.4" />
<PackageVersion Include="Dom.Mediator" Version="1.0.4" />
<PackageReference Include="Dom.Mediator" />
paket add Dom.Mediator --version 1.0.4
#r "nuget: Dom.Mediator, 1.0.4"
#:package Dom.Mediator@1.0.4
#addin nuget:?package=Dom.Mediator&version=1.0.4
#tool nuget:?package=Dom.Mediator&version=1.0.4
Dom.Mediator
Dom.Mediator is a clean, minimal implementation of the Mediator pattern for .NET, focused on:
- ✅ Request/Response messaging (CQRS)
- ✅ Composable pipeline behaviors (validation, logging, etc.)
- ✅ Built-in Result Pattern handling with typed errors
- ✅ Lightweight with minimal dependencies
- ✅ Perfect fit for Minimal APIs or Clean Architecture
📦 Install via NuGet
dotnet add package Dom.Mediator
🚀 Quick Example
Commands (Actions without return values)
Define your command:
public record CreateTaskCommand(string Title, string? Description) : ICommand;
Create a handler:
public class CreateTaskHandler : ICommandHandler<CreateTaskCommand>
{
public Task<Result> Handle(CreateTaskCommand command, CancellationToken cancellationToken)
{
// Perform action logic here
return Task.FromResult(Result.Success());
}
}
Queries (Read operations with return values)
Define your query:
public record GetTasksQuery() : IQuery<List<TaskItem>>;
Create a handler:
public class GetTasksHandler : IQueryHandler<GetTasksQuery, List<TaskItem>>
{
public Task<Result<List<TaskItem>>> Handle(GetTasksQuery query, CancellationToken cancellationToken)
{
var tasks = GetTasksFromStore(); // Your logic here
return Task.FromResult(Result<List<TaskItem>>.Success(tasks));
}
}
Setup with Dependency Injection
builder.Services.AddMediator(config =>
{
config.RegisterHandlers(typeof(Program).Assembly);
// Add pipeline behaviors - automatically handles both queries and commands
config.AddBehaviour(typeof(LoggingBehaviour<,>)); // For queries (request/response)
config.AddBehaviour(typeof(LoggingBehaviour<>)); // For commands (no response)
});
📚 Samples & Tests
Check out the samples directory for complete working examples:
🧪 Testing: See the tests directory for unit tests, coverage reports, and testing documentation.
📊 Coverage: Here also the coverage report related to the latest version.
🌐 Minimal API Sample
A complete ASP.NET Core Minimal API implementation demonstrating:
- Task Management API with CQRS pattern
- Command & Query handlers with validation
- Pipeline behaviors for logging and cross-cutting concerns
- Result pattern integration with HTTP responses
- Swagger documentation and endpoint configuration
cd samples/Dom.Mediator.Samples.MinimalApi
dotnet run
⚙️ Built-in Features
- 🧱 Pipeline Behaviors: Add logging, validation, error handling in a clean chain
- 🎯 Result Pattern: Native success/failure with structured error codes (
Result<T>andResult) - 🔄 Auto-Handler Discovery: Reflective scanning via assembly registration
- 💡 CQRS Support: Separate Command and Query handling with distinct interfaces
- 🏗️ Dependency Injection: Built-in support for Microsoft DI container
🧩 Pipeline Behaviors
Add behaviors to intercept and process requests using a unified registration method:
// Single registration method for both query and command behaviors
config.AddBehaviour(typeof(LoggingBehaviour<,>)); // For queries (2 generic parameters)
config.AddBehaviour(typeof(ValidationBehaviour<>)); // For commands (1 generic parameter)
The mediator automatically detects whether your behavior is for:
- Queries (request/response) - behaviors with 2 generic type parameters
- Commands (no response) - behaviors with 1 generic type parameter
Query Behavior Interface (Request/Response):
public interface IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
Task<Result<TResponse>> Handle(
TRequest request,
CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next);
}
Command Behavior Interface (No Response):
public interface IPipelineBehavior<TCommand>
where TCommand : ICommand
{
Task<Result> Handle(
TCommand command,
CancellationToken cancellationToken,
CommandHandlerDelegate next);
}
Example Behavior Implementation:
// Query behavior (2 generic parameters)
public class LoggingBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly ILogger<LoggingBehaviour<TRequest, TResponse>> _logger;
public LoggingBehaviour(ILogger<LoggingBehaviour<TRequest, TResponse>> logger)
=> _logger = logger;
public async Task<Result<TResponse>> Handle(
TRequest request,
CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next)
{
_logger.LogInformation("Handling {RequestType}", typeof(TRequest).Name);
var response = await next();
_logger.LogInformation("Handled {RequestType}", typeof(TRequest).Name);
return response;
}
}
// Command behavior (1 generic parameter)
public class LoggingBehaviour<TCommand> : IPipelineBehavior<TCommand>
where TCommand : ICommand
{
private readonly ILogger<LoggingBehaviour<TCommand>> _logger;
public LoggingBehaviour(ILogger<LoggingBehaviour<TCommand>> logger)
=> _logger = logger;
public async Task<Result> Handle(
TCommand command,
CancellationToken cancellationToken,
CommandHandlerDelegate next)
{
_logger.LogInformation("Handling {CommandType}", typeof(TCommand).Name);
var response = await next();
_logger.LogInformation("Handled {CommandType}", typeof(TCommand).Name);
return response;
}
}
📜 License
Dom.Mediator is MIT licensed.
👤 Author
Made with ❤️ by André Dominic Santacroce
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net8.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 |
|---|---|---|
| 1.0.4 | 768 | 1/27/2026 |
| 1.0.3 | 229 | 12/22/2025 |
| 1.0.2 | 488 | 12/10/2025 |
| 1.0.1 | 489 | 8/6/2025 |
| 1.0.0 | 232 | 7/10/2025 |
| 1.0.0-preview.10 | 268 | 8/6/2025 |
| 1.0.0-preview.9 | 104 | 8/1/2025 |
| 1.0.0-preview.8 | 223 | 7/16/2025 |
| 1.0.0-preview.7 | 199 | 7/16/2025 |
| 1.0.0-preview.6 | 195 | 7/16/2025 |
| 1.0.0-preview.5 | 197 | 7/14/2025 |
| 1.0.0-preview.4 | 154 | 7/11/2025 |
| 1.0.0-preview.3 | 145 | 7/11/2025 |
| 1.0.0-preview.2 | 151 | 7/11/2025 |
| 1.0.0-preview.1 | 186 | 7/10/2025 |