# murillofilho86/mediatr architecture

> Architecture diagram of the GitHub repository murillofilho86/mediatr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/murillofilho86/mediatr
- Repository: https://github.com/murillofilho86/mediatr

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client
    Client["Client Application"]:::external

    %% Contracts Layer
    subgraph "Contracts Layer"
        IRequest["IRequest<TResponse>"]:::contracts
        INotification["INotification"]:::contracts
        IStreamRequest["IStreamRequest<T>"]:::contracts
        Unit["Unit"]:::contracts
    end

    %% Mediator Core
    subgraph "Mediator Core"
        IMediator["IMediator"]:::core
        ISender["ISender"]:::core
        IPublisher["IPublisher"]:::core
        Mediator["Mediator"]:::core
    end

    %% Handlers & Wrappers
    subgraph "Handlers & Wrappers"
        IRequestHandler["IRequestHandler<,>"]:::handlers
        INotificationHandler["INotificationHandler<>"]:::handlers
        IStreamRequestHandler["IStreamRequestHandler<>"]:::handlers
        RequestWrapper["RequestHandlerWrapper"]:::handlers
        NotificationWrapper["NotificationHandlerWrapper"]:::handlers
        StreamWrapper["StreamRequestHandlerWrapper"]:::handlers
    end

    %% Pipeline Behaviors
    subgraph "Pipeline Behaviors"
        PreProcessorBehavior["RequestPreProcessorBehavior"]:::pipeline
        PostProcessorBehavior["RequestPostProcessorBehavior"]:::pipeline
        ExceptionProcessorBehavior["RequestExceptionProcessorBehavior"]:::pipeline
        ExceptionActionProcessorBehavior["RequestExceptionActionProcessorBehavior"]:::pipeline
        IRequestPreProcessor["IRequestPreProcessor<>"]:::pipeline
        IRequestPostProcessor["IRequestPostProcessor<>"]:::pipeline
        IRequestExceptionHandler["IRequestExceptionHandler<>"]:::pipeline
        IRequestExceptionAction["IRequestExceptionAction<>"]:::pipeline
        IRequestExceptionHandlerState["IRequestExceptionHandlerState"]:::pipeline
    end

    %% Notification Publishers
    subgraph "Notification Publishers"
        ForeachPublisher["ForeachAwaitPublisher"]:::pipeline
        TaskWhenAllPublisher["TaskWhenAllPublisher"]:::pipeline
        HandlerExecutor["NotificationHandlerExecutor"]:::pipeline
    end

    %% Dependency Injection Integration
    subgraph "Dependency Injection"
        ServiceCollectionExtensions["ServiceCollectionExtensions"]:::di
        MediatrServiceConfiguration["MediatrServiceConfiguration"]:::di
        RequestExceptionActionStrategy["RequestExceptionActionProcessorStrategy"]:::di
    end

    %% Internal Utilities
    subgraph "Internal Utilities"
        HandlersOrderer["HandlersOrderer"]:::internal
        ObjectDetails["ObjectDetails"]:::internal
    end

    %% Service Registration
    ServiceRegistrar["ServiceRegistrar"]:::registration

    %% Relationships
    Client -->|"Send()/Publish()"| IMediator
    IMediator -->|"calls"| Mediator
    Mediator -->|"uses DI to resolve"| ServiceRegistrar
    ServiceRegistrar -->|"registers open generics"| ServiceCollectionExtensions
    ServiceCollectionExtensions --> MediatrServiceConfiguration
    Mediator -->|"dispatch to"| PreProcessorBehavior
    PreProcessorBehavior --> RequestWrapper
    Mediator -->|"dispatch to"| PostProcessorBehavior
    Mediator -->|"dispatch to"| ExceptionProcessorBehavior
    Mediator -->|"dispatch to"| ExceptionActionProcessorBehavior
    Mediator -->|"for streams"| StreamWrapper
    Mediator -->|"for notifications"| HandlerExecutor
    HandlerExecutor --> ForeachPublisher
    HandlerExecutor --> TaskWhenAllPublisher
    HandlerExecutor --> NotificationWrapper
    IRequestHandler --> RequestWrapper
    INotificationHandler --> NotificationWrapper
    IStreamRequestHandler --> StreamWrapper
    IRequest --> IRequestHandler
    INotification --> INotificationHandler
    IStreamRequest --> IStreamRequestHandler
    Mediator --> IPublisher
    Mediator --> ISender
    ServiceRegistrar --> RequestExceptionActionStrategy
    Mediator --> HandlersOrderer
    Mediator --> ObjectDetails

    %% Click Events
    click IRequest "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR.Contracts/IRequest.cs"
    click INotification "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR.Contracts/INotification.cs"
    click IStreamRequest "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR.Contracts/IStreamRequest.cs"
    click Unit "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR.Contracts/Unit.cs"
    click IMediator "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/IMediator.cs"
    click ISender "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/ISender.cs"
    click IPublisher "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/IPublisher.cs"
    click Mediator "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Mediator.cs"
    click IRequestHandler "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/IRequestHandler.cs"
    click INotificationHandler "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/INotificationHandler.cs"
    click IStreamRequestHandler "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/IStreamRequestHandler.cs"
    click RequestWrapper "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Wrappers/RequestHandlerWrapper.cs"
    click NotificationWrapper "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Wrappers/NotificationHandlerWrapper.cs"
    click StreamWrapper "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs"
    click PreProcessorBehavior "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs"
    click PostProcessorBehavior "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs"
    click ExceptionProcessorBehavior "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs"
    click ExceptionActionProcessorBehavior "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/RequestExceptionActionProcessorBehavior.cs"
    click IRequestPreProcessor "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/IRequestPreProcessor.cs"
    click IRequestPostProcessor "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/IRequestPostProcessor.cs"
    click IRequestExceptionHandler "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/IRequestExceptionHandler.cs"
    click IRequestExceptionAction "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/IRequestExceptionAction.cs"
    click IRequestExceptionHandlerState "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Pipeline/IRequestExceptionHandlerState.cs"
    click ForeachPublisher "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/NotificationPublishers/ForeachAwaitPublisher.cs"
    click TaskWhenAllPublisher "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/NotificationPublishers/TaskWhenAllPublisher.cs"
    click HandlerExecutor "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/NotificationHandlerExecutor.cs"
    click ServiceCollectionExtensions "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/MicrosoftExtensionsDI/ServiceCollectionExtensions.cs"
    click MediatrServiceConfiguration "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs"
    click RequestExceptionActionStrategy "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/MicrosoftExtensionsDI/RequestExceptionActionProcessorStrategy.cs"
    click HandlersOrderer "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Internal/HandlersOrderer.cs"
    click ObjectDetails "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Internal/ObjectDetails.cs"
    click ServiceRegistrar "https://github.com/murillofilho86/mediatr/blob/master/src/MediatR/Registration/ServiceRegistrar.cs"

    %% Styles
    classDef contracts fill:#cce5ff,stroke:#0056b3,color:#004085
    classDef core fill:#d4edda,stroke:#155724,color:#155724
    classDef handlers fill:#fff3cd,stroke:#856404,color:#856404
    classDef pipeline fill:#fff3cd,stroke:#856404,color:#856404
    classDef di fill:#e2e3e5,stroke:#6c757d,color:#383d41
    classDef internal fill:#f8f9fa,stroke:#6c757d,color:#495057,stroke-dasharray: 5 5
    classDef registration fill:#f5e6ff,stroke:#6f42c1,color:#4e0d82
    classDef external fill:#ffffff,stroke:#000000,color:#000000,stroke-width:2px
```
