# mohammedgamal17/mediator architecture

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

- Interactive diagram: https://gitdiagram.com/mohammedgamal17/mediator
- Repository: https://github.com/mohammedgamal17/mediator

## Overview

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

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Layer" 
        ClientApp["Client Application"]:::client
    end

    %% DI Container
    subgraph "DI Container"
        DI["DI Container"]:::di
    end

    %% MoMediatoR Library
    subgraph "MoMediatoR Library"
        subgraph "Core Interfaces"
            IMoMediator["IMoMediatoR"]:::core
            IRequestHandler["IRequestHandler<TReq,TRes>"]:::core
            INotificationHandler["INotificationHandler<TNotif>"]:::core
        end
        subgraph "Infrastructure / Mediator"
            MoMediator["MoMediatoR"]:::infra
            HandlerRegistry["HandlerRegistry"]:::infra
            DelegateFactory["DelegateFactory"]:::infra
            ExpressionTreeBuilder["ExpressionTreeBuilder"]:::infra
            DelegateCache["DelegateCache"]:::infra
        end
        subgraph "Extensions"
            ServiceCollectionExt["MediatorServiceCollectionExtensions"]:::ext
            TaskExt["TaskExtensions"]:::ext
        end
        GlobalUsings["GlobalUsing.cs"]:::infra
        CoreCsproj["MediatoR.csproj"]:::infra
    end

    %% External Handlers
    subgraph "Handler Types (External)"
        ExternalHandlers["Request/Notification Handler Implementations"]:::external
    end

    %% Support Projects
    subgraph "Support Projects"
        Benchmarks["MoMediatoR.Benchmarks"]:::support
        Tests["MoMediatoR.Tests"]:::support
    end

    %% Documentation
    subgraph "Documentation"
        README["README.md"]:::docs
        LICENSE["LICENSE"]:::docs
        Gitignore[".gitignore"]:::docs
    end

    %% Connections
    ClientApp -->|"Send()/Publish()"| MoMediator
    MoMediator -->|"lookup"| HandlerRegistry
    HandlerRegistry -->|"get delegate"| DelegateCache
    DelegateCache -->|"execute Handle()"| ExternalHandlers
    ExternalHandlers -->|"Handle() result"| DelegateCache
    DelegateCache -->|"return result"| MoMediator
    MoMediator -->|"return response"| ClientApp

    DI -->|"Assembly scanning & registration"| ServiceCollectionExt
    ServiceCollectionExt -->|"register IMoMediatoR"| IMoMediator
    ServiceCollectionExt -->|"register handlers"| IRequestHandler
    ServiceCollectionExt -->|"register handlers"| INotificationHandler
    ServiceCollectionExt --> DI

    ServiceCollectionExt --> DI
    TaskExt -.-> MoMediator

    %% Support flows
    Benchmarks -.-> MoMediator
    Tests -.-> MoMediator

    %% Documentation usage
    README -.-> CoreCsproj
    LICENSE -.-> CoreCsproj
    Gitignore -.-> CoreCsproj

    %% Click Events
    click IMoMediator "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Core/Interfaces/IMoMediatoR.cs"
    click IRequestHandler "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Core/Interfaces/IRequestHandler.cs"
    click INotificationHandler "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Core/Interfaces/INotificationHandler.cs"
    click MoMediator "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Infrastructure/Mediator/MoMediatoR.cs"
    click ServiceCollectionExt "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Extensions/MediatorServiceCollectionExtensions.cs"
    click TaskExt "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/Extensions/TaskExtensions.cs"
    click GlobalUsings "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/GlobalUsing.cs"
    click CoreCsproj "https://github.com/mohammedgamal17/mediator/blob/main/MediatoR/MediatoR.csproj"
    click Benchmarks "https://github.com/mohammedgamal17/mediator/blob/main/MoMediatoR.Benchmarks/MoMediatoR.Benchmarks.csproj"
    click Tests "https://github.com/mohammedgamal17/mediator/blob/main/MoMediatoR.Tests/MoMediatoR.Tests.csproj"
    click README "https://github.com/mohammedgamal17/mediator/blob/main/README.md"
    click LICENSE "https://github.com/mohammedgamal17/mediator/tree/main/LICENSE"
    click Gitignore "https://github.com/mohammedgamal17/mediator/blob/main/.gitignore"

    %% Styles
    classDef client fill:#ffa500,stroke:#333,stroke-width:1px
    classDef di fill:#8fbc8f,stroke:#333,stroke-width:1px
    classDef library fill:#87cefa,stroke:#333,stroke-width:1px
    classDef core fill:#add8e6,stroke:#333,stroke-width:1px
    classDef infra fill:#b0e0e6,stroke:#333,stroke-width:1px
    classDef ext fill:#afeeee,stroke:#333,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
    classDef support fill:#f0f0f0,stroke:#333,stroke-width:1px
    classDef docs fill:#eeeeee,stroke:#333,stroke-width:1px
```
