XForge.MediatR.SourceGenerators
1.0.7
dotnet add package XForge.MediatR.SourceGenerators --version 1.0.7
NuGet\Install-Package XForge.MediatR.SourceGenerators -Version 1.0.7
<PackageReference Include="XForge.MediatR.SourceGenerators" Version="1.0.7" />
<PackageVersion Include="XForge.MediatR.SourceGenerators" Version="1.0.7" />
<PackageReference Include="XForge.MediatR.SourceGenerators" />
paket add XForge.MediatR.SourceGenerators --version 1.0.7
#r "nuget: XForge.MediatR.SourceGenerators, 1.0.7"
#:package XForge.MediatR.SourceGenerators@1.0.7
#addin nuget:?package=XForge.MediatR.SourceGenerators&version=1.0.7
#tool nuget:?package=XForge.MediatR.SourceGenerators&version=1.0.7
XForge.MediatR
![]()
Simple, fluent, in-process mediator for .NET with CQRS, pipeline behaviors, and local events.
Why XForge.MediatR?
A modern alternative to MediatR with explicit CQRS, ValueTask contracts, behavior ordering, built-in resilience, and TestableMediator — all under MIT license.
| Feature | XForge.MediatR | MediatR |
|---|---|---|
ValueTask on contracts |
✅ | ❌ |
Explicit CQRS (ICommand, IQuery) |
✅ | ❌ |
| Behavior ordering & filtering | ✅ | ❌ |
TestableMediator (no DI) |
✅ | ❌ |
Result<T> monad |
✅ | ❌ |
| Retry + Circuit Breaker + Rate Limiter | ✅ | ❌ |
| Roslyn analyzer | ✅ | ❌ |
| OpenTelemetry package | ✅ | ❌ |
| Source generator for handlers | ✅ | ❌ |
| MIT license (no license key) | ✅ | ❌ |
Quick Start
dotnet add package XForge.MediatR.DependencyInjection
// 1. Define a query
public record GetOrder(int OrderId) : IQuery<OrderDto>;
public record OrderDto(int Id, string Customer, decimal Total);
// 2. Implement the handler
public class GetOrderHandler : IRequestHandler<GetOrder, OrderDto>
{
public ValueTask<OrderDto> Handle(GetOrder request, CancellationToken ct)
=> ValueTask.FromResult(new OrderDto(request.OrderId, "Acme Corp", 250.00m));
}
// 3. Register and use
builder.Services.AddXForgeMediator(cfg =>
{
cfg.RegisterHandlersFrom<Program>();
cfg.UseDefaults(); // Validation + Logging + Metrics
});
app.MapGet("/orders/{id}", async (int id, IXForgeMediator mediator) =>
Results.Ok(await mediator.Send(new GetOrder(id))));
Packages
| Package | Description | Dependencies |
|---|---|---|
XForge.MediatR |
Core mediator + pipeline + behaviors | DI.Abstractions, Logging.Abstractions |
XForge.MediatR.Abstractions |
Contracts and interfaces (zero dependencies) | None |
XForge.MediatR.DependencyInjection |
DI extensions + fluent configuration API | DI.Abstractions |
XForge.MediatR.Testing |
TestableMediator for unit tests without DI |
None (Abstractions only) |
XForge.MediatR.SourceGenerators |
Compile-time handler discovery + Roslyn analyzer | None (netstandard2.0) |
XForge.MediatR.OpenTelemetry |
Traces + metrics via OpenTelemetry | OpenTelemetry |
XForge.MediatR.AspNetCore |
ASP.NET Core integration (model binding) | AspNetCore.Mvc.Core |
XForge.MediatR.Validation |
FluentValidation adapter | FluentValidation |
XForge.MediatR.EntityFrameworkCore |
EF Core transaction behavior | EntityFrameworkCore |
XForge.MediatR.Outbox |
Outbox pattern for deferred event publishing | None (Abstractions only) |
Key Features
- CQRS Explícito —
ICommand<T>,IQuery<T>,IEventwith semantic separation - Flat Pipeline — behaviors with explicit ordering (
IOrdered) and filtering (UseFor<T>,UseWhen) - Built-in Behaviors — validation, logging, metrics, retry, circuit breaker, rate limiter, concurrency limiter, exception handling, pre/post processors
- Result<T> — typed success/failure without exceptions
- Streaming —
IStreamRequest<T>withIAsyncEnumerable<T>and pipeline support - TestableMediator — builder pattern for testing without DI container
- Source Generator — compile-time handler registration, zero reflection at startup
- Roslyn Analyzer —
XFMR001(duplicate handler),XFMR002(void mismatch),XFMR003(event handler return type) - OpenTelemetry — vendor-agnostic traces and metrics
- Multi-TFM — targets
net8.0,net9.0,net10.0
Built-in Behaviors
| Behavior | Order | Description |
|---|---|---|
ConcurrencyLimiter |
-325 | SemaphoreSlim concurrency control |
RateLimiter |
-320 | Fixed-window rate limiting |
CircuitBreaker |
-310 | Opens circuit after consecutive failures |
Retry |
-300 | Exponential/linear/constant backoff |
Authorization |
-280 | Handler-level authorization checks |
Idempotency |
-290 | Idempotent request processing |
MultiTenant |
-260 | Tenant validation |
ExceptionHandling |
-250 | Typed exception handlers |
Validation |
-200 | FluentValidation integration |
PrePostProcessors |
-150 | Pre/post handler hooks |
Logging |
-100 | High-performance structured logging |
Diagnostics |
-75 | Activity enrichment |
PerformanceMetrics |
-50 | Duration measurement + Activity spans |
Audit |
-40 | Audit trail via IAuditSink |
Caching |
-30 | Query result caching |
Transaction |
-15 | TransactionScope for commands |
Performance
Benchmarked on .NET 10.0.8, x64 RyuJIT AVX2 against MediatR 14.1.0 and Mediator 3.0.2 (source-generator):
| Operation | XForge.MediatR | MediatR | Mediator (source-gen) |
|---|---|---|---|
| Send | 61 ns / 120 B | 72 ns / 296 B | 21 ns / 96 B |
| Publish | 161 ns / 88 B | 392 ns / 1248 B | 27 ns / 0 B |
| Pipeline (0 beh) | 79 ns / 120 B | 74 ns / 296 B | — |
| Pipeline (3 beh) | 671 ns / 1024 B | 150 ns / 608 B | — |
| Cold Start | ~6 µs | ~174 µs | ~14 µs |
Highlights:
- Send 1.18x faster than MediatR with 59% less allocation (120 B vs 296 B)
- Publish 2.43x faster than MediatR with 14x less allocation
- Cold Start 29x faster than MediatR — ideal for serverless
- Zero tracing overhead — ActivitySource short-circuits when no listeners
See Performance & Benchmarks for full details.
Documentation
- Manual (pt-br) — Complete Portuguese manual with 25 sections
- Manual (en-us) — Complete English manual
- CHANGELOG.md — Release history
- Architecture — Package dependency graph and design decisions
Requirements
| Requirement | Version |
|---|---|
| .NET SDK | 8.0+ |
| C# | 12+ |
| IDE | Visual Studio 2022 17.8+, Rider 2023.3+, or VS Code with C# extension |
License
MIT — Copyright (c) XForge
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.