AB
abbasmhd/Modiator
**Modiator** - A high-performance MediatR-style mediator library for .NET using C# Source Generators to eliminate runtime reflection while providing compile-time safety and automatic DI registration.
Modiator
A MediatR-style mediator library for .NET, built from scratch with C# Source Generators for high performance and compile-time safety.
Solution Structure
Modiator.Core— Core abstractions and the main mediator implementationModiator.SourceGenerator— Roslyn source generator for compile-time dispatcher generation and DI registrationModiator.Tests— Unit tests for core and generated codeModiator.SampleApp— Sample app demonstrating advanced usage and DI integration
Getting Started
- Build the solution:
dotnet build - Add your own requests, notifications, handlers, and pipeline behaviors in your application.
- Register everything with a single line:
services.AddModiator();
Key Features
- No runtime reflection for handler discovery
- Compile-time generated dispatcher for fast request/notification handling
- Pipeline behaviors for cross-cutting concerns
- Source-generated DI registration with filtering and lifetime options
Advanced Usage
Source-Generated DI Registration
You can register all handlers, behaviors, and the dispatcher with a single call:
services.AddModiator();Customization Options
You can filter which types are registered and set default lifetimes:
services.AddModiator(options => {
// Only register types in a specific namespace
options.Filter = t => t.Namespace != null && t.Namespace.StartsWith("MyApp.Handlers");
// Set default lifetimes
options.DefaultHandlerLifetime = ServiceLifetime.Scoped;
options.DefaultBehaviorLifetime = ServiceLifetime.Singleton;
options.DefaultNotificationHandlerLifetime = ServiceLifetime.Transient;
});- Filter: Only types passing the filter will be registered.
- Lifetimes: Each category (handler, behavior, notification handler) can have its own default lifetime (
Transient,Scoped, orSingleton).
Example: Registering Only Your App's Handlers as Scoped
services.AddModiator(options => {
options.Filter = t => t.Namespace != null && t.Namespace.StartsWith("MyApp.Handlers");
options.DefaultHandlerLifetime = ServiceLifetime.Scoped;
});How It Works
- The source generator discovers all handlers, notification handlers, and pipeline behaviors at compile time.
- It generates a dispatcher and a DI registration extension method (
AddModiator). - You can use the options to control which types are registered and their lifetimes.
- No manual registration is needed for any handler or behavior!
See the Modiator.SampleApp for a full demonstration.
See Also
- Sample App README — for a detailed walkthrough and output examples
prd.mdfor a deep dive into the architecture and design.
On this page
Contributors
MIT License
Created June 19, 2025
Updated June 19, 2025