Mediator is a high performance implementation of the Mediator pattern in .NET using source generators.
The mediator pattern addresses the need to implement cross-cutting concerns like logging and metrics while avoiding constructor bloat from numerous injected dependencies. This implementation uses source generators to produce code at compile time rather than relying on reflection or runtime code generation. The generator creates DI registration code, implements the IMediator interface, monomorphizes Send methods for requests and commands, and generates diagnostics to catch configuration errors during development. For larger projects, it builds fast dictionary lookups for methods taking object or generic arguments.
Developers should choose this tool if they need Native AOT support, care about cold start performance in serverless or edge scenarios, or want build-time error detection instead of runtime failures. The tool suits projects targeting .NET Standard 2.0 or .NET 8 where performance is a priority. The README explicitly compares it to MediatR, noting that this implementation delivers better performance while maintaining a similar API. Unlike MediatR, it provides full Native AOT support without reflection and emphasizes non-pessimization by making slower configurations opt-in. Users can work with either the IMediator interface or the concrete Mediator class, with the latter offering better performance characteristics.
The project maintains a stable API that changes only for good reason and follows semantic versioning strictly, reducing the patching burden on adopters. The source generator includes diagnostics that warn when handlers are not defined for requests, catching mistakes during development. The library is distributed as two NuGet packages: Mediator.SourceGenerator for executable projects and Mediator.Abstractions for projects defining messages and handlers, with clear guidance that the generator should not be installed in every layer of a deployed artifact.