MediatorLite

CI MediatorLite MediatorLite.SourceGeneration License: MIT .NET

A lightweight, high-performance mediator for .NET with zero-reflection dispatch and compile-time source generation.

Get Started View on GitHub


Why MediatorLite?

MediatorLite implements the Mediator pattern for .NET applications, decoupling request senders from their handlers. Unlike other mediator libraries, MediatorLite uses Roslyn source generators to discover and register handlers at compile time — no runtime reflection required.

Feature MediatorLite Traditional Mediators
Handler discovery Compile-time Runtime reflection
Dispatch overhead Near-zero Reflection-based
Native AOT support Yes Limited
Assembly trimming Yes Limited
Startup cost None Assembly scanning

Key Features

  • Zero-Reflection Dispatch — Handler registration and dispatch are generated at compile time via Roslyn source generators.
  • High PerformanceValueTask-based handlers with minimal overhead and no boxing.
  • Pipeline Behaviors — Composable middleware for cross-cutting concerns (logging, validation, caching, etc.).
  • Notifications — Pub/sub pattern with configurable execution strategies: Sequential, Parallel, and StopOnFirst.
  • Built-in Validation — First-class support for DataAnnotations and custom IValidator<T> implementations.
  • Observability — Structured logging and OpenTelemetry tracing out of the box.
  • DI Native — Integrates directly with Microsoft.Extensions.DependencyInjection.
  • Native AOT & Trimming — Works with .NET’s native AOT compilation and assembly trimming.

Installation

Install the core library plus the optional source generator:

dotnet add package MediatorLite
dotnet add package MediatorLite.SourceGeneration   # Recommended

Or for manual registration only:

dotnet add package MediatorLite

For shared contracts libraries (requests, notifications, and validation contracts only):

dotnet add package MediatorLite.Abstractions

MediatorLite already depends on MediatorLite.Abstractions, so application projects that install MediatorLite get abstractions transitively.


Quick Example

1. Define a request and handler:

public record GetUserQuery(int Id) : IRequest<User>;

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, User>
{
    public async ValueTask<User> HandleAsync(
        GetUserQuery request,
        CancellationToken cancellationToken = default)
    {
        return await _userRepository.GetByIdAsync(request.Id, cancellationToken);
    }
}

2. Register services (source-generated):

services
    .AddGeneratedHandlers()
    .AddMediatorLite();

3. Send a request:

var user = await mediator.SendAsync(new GetUserQuery(42));

That’s it. The source generator discovers GetUserQueryHandler at compile time — no attributes, no assembly scanning.


Documentation

Page Description
Quick Start Install the library and set up your first request, handler, and notification in minutes.
Pipeline Behaviors Compose reusable middleware for logging, validation, caching, and other cross-cutting concerns.
Validation Use DataAnnotations or custom IValidator<T> validator to validate requests before they reach handlers.
Notifications Publish events to multiple handlers with Sequential, Parallel, or StopOnFirst execution strategies.
Observability Configure built-in structured logging and OpenTelemetry tracing.
Benchmarks Performance comparisons against MediatR across request dispatch, pipeline behaviors, and notifications.
Migrating from MediatR Step-by-step guide and interface mapping for teams moving from MediatR.
Contributing How to build, test, and contribute to MediatorLite.

License

MediatorLite is released under the MIT License.