How mohammedgamal17/mediator works
Project Type and Purpose
• MoMediator is an open-source C# library implementing the Mediator pattern.
• Its goal is to decouple request/notification producers from their handlers, optimize runtime performance (through compiled delegate caching), and integrate cleanly with any .NET DI container.
File-Structure Analysis
• Top-level solution (MediatoR.sln) with three projects:
• MediatoR (the core library)
• MoMediatoR.Benchmarks (performance testing harness)
• MoMediatoR.Tests (unit tests)
• MediatoR project subfolders:
• Core/Interfaces – IRequest<T>, INotification, IRequestHandler<,>, INotificationHandler<>
• Infrastructure/Mediator – concrete MoMediatoR implementation, handler‐lookup, dispatch logic
• Extensions – DI registration helpers (assembly scanning, marker types), Task extensions
• README describes architecture, public API, usage examples, and DI registration.
Key Architecture Concepts (from README)
• At startup, all IRequestHandler<> and INotificationHandler<> types are discovered (via scanning assemblies or marker types) and cached.
• Handler invocation path uses compiled delegates (no reflection in hot path) for max performance.
• Consumers interact only with IMoMediatoR (has Send<TResponse> and Publish<TNotification> methods).
• CancellationToken propagation, support for sync/async handlers, generic requests and notifications.
Main Components & Their Relationships
A. Consumer Layer (“Client Application” or any calling code)
• Calls IMoMediatoR.Send(request) or .Publish(notification).
B. Dependency Injection Container
• Registers IMoMediatoR implementation and all handlers via Extensions.
C. MoMediatoR Library
1. Core Interfaces
• IRequest<TResponse>, INotification
• IRequestHandler<TReq, TRes>, INotificationHandler<TNotif>
2. Infrastructure / Mediator Implementation
• MoMediatoR: entry point implementing IMoMediatoR
• HandlerRegistry: stores mapping from request/notification types to handler delegates
• DelegateFactory / ExpressionTreeBuilder: builds and caches delegates for Handle() methods
• Caching layer: internal caches for handler types and delegates
3. Extensions
• MediatorServiceCollectionExtensions: AddMoMediatoR(…) overloads for DI registration
• TaskExtensions: helper methods for task/async boilerplate
D. Handler Implementations
• User-provided classes implementing IRequestHandler<> or INotificationHandler<>
• Reside in client assemblies but discovered and invoked by the mediator