How murillofilho86/mediatr works
Project Type and Purpose
• This is an open-source .NET library implementing the Mediator pattern (in-process messaging).
• It provides contracts (IRequest, INotification, IStreamRequest), a core mediator implementation, pipeline behaviors, and integration with Microsoft’s DI.
• Intended for use as a framework dependency in client applications (web APIs, console apps, etc.) to decouple senders from handlers.
File Structure Analysis
• Top-level:
• src/ – main code, split into two projects:
– MediatR.Contracts (defines interfaces and basic message types)
– MediatR (core Mediator, pipelines, DI extensions, internal helpers, wrappers)
• samples/ – consumer examples using various DI containers and features.
• test/ – benchmarks and unit/integration tests for all major flows.
• Build and CI config at root (.github/workflows, Build.ps1, Push.ps1, NuGet.Config).
• Key patterns:
• Mediator core orchestrates Send and Publish calls.
• Pipeline behaviors (pre/post processors, exception handlers).
• Dependency registration via ServiceCollectionExtensions.
• Wrapper classes to adapt handlers to a uniform invocation model.
README Insights
• Describes supported message types (commands/queries, notifications, streams).
• Explains DI registration API (AddMediatR) and how open generics are wired up.
• Mentions extensibility hooks: AddBehavior, AddStreamBehavior, AddRequestPreProcessor, etc.
Main Components
a. Contracts Layer (MediatR.Contracts):
• Interfaces: IRequest<TResponse>, INotification, IStreamRequest<T>
b. Mediator Core (Mediator class in MediatR):
• Entry point implementing IMediator, ISender, IPublisher
• Dispatches to handlers and pipeline
c. Handlers and Wrappers:
• Concrete IRequestHandler<,>, INotificationHandler<>, IStreamRequestHandler<>
• Wrapper classes under Wrappers/ adapt concrete handlers for uniform execution
d. Pipeline Behaviors (Pipeline folder):
• Pre/Post processors, exception handlers as IPipelineBehavior<,>, IRequestPreProcessor<>, etc.
• Notification publishers in NotificationPublishers/
e. DI Integration (MicrosoftExtensionsDI folder):
• ServiceCollectionExtensions configures services
• Strategies for exception actions, open-generic registration
f. Internal Utilities (Internal folder):
• Handler order resolution, reflection caching (ObjectDetails)
Relationships & Interactions
• Client code calls IMediator.Send(request) or .Publish(notification).
• Mediator locates handler(s) via DI container → wrappers → pipeline behaviors → actual handler invocation.
• Pipeline behaviors execute in a chain (pre-processor → handler → post-processor → exception handlers).
• For notifications: publisher selects strategy (ForeachAwaitPublisher or TaskWhenAllPublisher) to fan out to multiple handlers.
• DI container resolves all handler implementations and behaviors at startup via AddMediatR.
Architectural Patterns & Principles
• Mediator pattern to decouple requestors from handlers.
• Open generics for handler and behavior registration.
• Pipeline pattern to inject cross-cutting concerns (logging, validation, exception handling).
• Inversion of Control via Microsoft.Extensions.DependencyInjection.