# jbogard/mediatr architecture

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

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

## Overview

##### Main Components

a) Core Components:
- Mediator (Central Hub)
- Request/Response Pipeline
- Notification Pipeline
- Stream Pipeline
- Service Registration System

b) Key Interfaces:
- IMediator
- ISender
- IPublisher
- IRequest/IRequestHandler
- INotification/INotificationHandler
- IStreamRequest/IStreamRequestHandler

##### Architectural Patterns to Highlight

- Mediator Pattern (core pattern)
- Pipeline Pattern (for request/notification processing)
- Publisher/Subscriber Pattern (for notifications)
- Chain of Responsibility (for behaviors)

**[Client Application] → [MediatR Registration (DI)]**

**[ISender] →[Mediator]← [IPublisher]**

**[Pre-Processors] → [Behaviors] → [Handlers] → [Post-Processors]**

**[Request Handlers] [Notification Handlers] [Stream Handlers]**

##### Special Elements

- Exception Handling Pipeline
- Stream Processing Pipeline
- Notification Publishing Strategies

##### Detailed Component Relationships

**[Client] → [IMediator]**
- ↓
**[Pipeline Behaviors]**
- ↓
**[Pre-Processors]**
- ↓
**[Request/Notification Processing]**
- ↓
**[Post-Processors]**
- ↓
**[Response/Notification Dispatch]**

##### Important Notes

- Variance support in the generic handlers
- Different notification publishing strategies
- Pipeline behavior ordering
- Separation between synchronous and asynchronous flows
- Exception handling pipeline integration

##### Implementation Details

- Service registration flow
- Handler resolution process
- Pipeline behavior chaining
- Exception handling integration
- Stream processing specifics

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Layer"
        Client["Client Application"]
        DI["MediatR Registration"]:::registration
    end

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

    subgraph "Pipeline Layer"
        PreProc["Request Pre-Processing"]:::pipeline
        PostProc["Request Post-Processing"]:::pipeline
        ExceptionProc["Exception Processing"]:::pipeline
        PipelineBehavior["Pipeline Behavior"]:::pipeline
    end

    subgraph "Handler Layer"
        RequestHandler["Request Handler"]:::handler
        NotificationHandler["Notification Handler"]:::handler
        StreamHandler["Stream Handler"]:::handler
    end

    subgraph "Infrastructure"
        ServiceReg["Service Registrar"]:::infra
        HandlerWrapper["Request Handler Wrapper"]:::infra
        NotifWrapper["Notification Handler Wrapper"]:::infra
        StreamWrapper["Stream Handler Wrapper"]:::infra
        NotifPublisher["Notification Publishers"]:::infra
        NotifExecutor["Notification Handler Executor"]:::infra
    end

    %% Relationships
    Client --> DI
    DI --> Mediator
    Mediator --> ISender
    Mediator --> IPublisher
    Mediator --> IMediator

    ISender --> PipelineBehavior
    IPublisher --> NotifPublisher
    
    PipelineBehavior --> PreProc
    PreProc --> HandlerWrapper
    HandlerWrapper --> RequestHandler
    RequestHandler --> PostProc
    PipelineBehavior --> ExceptionProc

    NotifPublisher --> NotifExecutor
    NotifExecutor --> NotifWrapper
    NotifWrapper --> NotificationHandler

    StreamWrapper --> StreamHandler

    ServiceReg --> Mediator

    %% Click Events
    click Mediator "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Mediator.cs"
    click ISender "https://github.com/jbogard/MediatR/blob/master/src/MediatR/ISender.cs"
    click IPublisher "https://github.com/jbogard/MediatR/blob/master/src/MediatR/IPublisher.cs"
    click IMediator "https://github.com/jbogard/MediatR/blob/master/src/MediatR/IMediator.cs"
    click PreProc "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs"
    click PostProc "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs"
    click ExceptionProc "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs"
    click PipelineBehavior "https://github.com/jbogard/MediatR/blob/master/src/MediatR/IPipelineBehavior.cs"
    click RequestHandler "https://github.com/jbogard/MediatR/blob/master/src/MediatR/IRequestHandler.cs"
    click HandlerWrapper "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Wrappers/RequestHandlerWrapper.cs"
    click NotifWrapper "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Wrappers/NotificationHandlerWrapper.cs"
    click StreamWrapper "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs"
    click NotifPublisher "https://github.com/jbogard/MediatR/tree/master/src/MediatR/NotificationPublishers"
    click NotifExecutor "https://github.com/jbogard/MediatR/blob/master/src/MediatR/NotificationHandlerExecutor.cs"
    click ServiceReg "https://github.com/jbogard/MediatR/blob/master/src/MediatR/Registration/ServiceRegistrar.cs"
    click DI "https://github.com/jbogard/MediatR/blob/master/src/MediatR/MicrosoftExtensionsDI/ServiceCollectionExtensions.cs"

    %% Styles
    classDef core fill:#2374ab
    classDef interface fill:#457b9d
    classDef pipeline fill:#a8dadc
    classDef handler fill:#e63946
    classDef infra fill:#f1faee
    classDef registration fill:#1d3557

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Interfaces"]:::interface
        L3["Pipeline Components"]:::pipeline
        L4["Handlers"]:::handler
        L5["Infrastructure"]:::infra
        L6["Registration"]:::registration
    end
```
