# sakopov/dapper.ambientcontext architecture

> Architecture diagram of the GitHub repository sakopov/dapper.ambientcontext, 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/sakopov/dapper.ambientcontext
- Repository: https://github.com/sakopov/dapper.ambientcontext

## Overview

##### Main Components

a. Core Components:
- AmbientDbContext (Central Component)
- Storage System
- Connection Factory
- Context Factory
- Context Locator
- Query Proxy System

b. Integration Points:
- Dapper Integration
- Application Entry Points
- Database Connection

**Top Layer (Application Layer):**
- Show sample applications (Console & Web) integration points
- Demonstrate where context is initialized

**Middle Layer (Core Library):**
- AmbientDbContext as the central component
- Storage providers (AsyncLocal and LogicalCallContext)
- Context management components
- Query proxy system

**Bottom Layer (Infrastructure):**
- Database connection
- Dapper integration

##### Component Relationships

- Application → AmbientDbContext (Context initialization)
- AmbientDbContext → Storage Providers (Context storage)
- AmbientDbContext → Connection Factory (Database connectivity)
- Query Proxy → AmbientDbContext (Query execution)
- AmbientDbContext → Dapper (Underlying data access)

##### Detailed Component Breakdown

a. Storage Provider System:
- AsyncLocalContextStorage
- LogicalCallContextStorage
- IContextualStorage interface

b. Context Management:
- AmbientDbContextFactory
- AmbientDbContextLocator
- AmbientDbContextStorageProvider

c. Query Execution Flow:
- IAmbientDbContextQueryProxy
- Query execution pipeline
- Transaction handling

##### Implementation Notes

Highlight:
- Thread-safety mechanisms
- Async support
- Transaction scope
- Connection lifetime management

- The ambient nature of the context
- How the context flows through different parts of the application
- Transaction and connection management
- Integration with existing Dapper functionality

Remember to:
- Clear boundaries between components
- Direction of data/control flow
- Document any important assumptions or constraints

- How the ambient context pattern is implemented
- How database connections and transactions are managed
- How the library integrates with existing applications
- The flow of control through the system

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Application
        ConsoleApp["Console App"]:::app
        WebApp["Web App"]:::app
    end

    subgraph Core["Core Library"]
        AmbientContext["AmbientDbContext"]:::core
        ContextFactory["Context Factory"]:::core
        ContextLocator["Context Locator"]:::core
        StorageProvider["Storage Provider"]:::core
        
        subgraph Storage["Storage System"]
            AsyncLocal["AsyncLocal Storage"]:::storage
            LogicalCall["LogicalCall Storage"]:::storage
            IStorage["IContextualStorage"]:::storage
        end
        
        subgraph QuerySystem["Query Proxy System"]
            QueryProxy["Query Proxy"]:::query
            ProxyAsync["Proxy Async"]:::query
        end
    end

    subgraph Infrastructure
        ConnFactory["Connection Factory"]:::infra
        Dapper["Dapper"]:::infra
        DB[(Database)]:::db
    end

    %% Relationships
    ConsoleApp --> AmbientContext
    WebApp --> AmbientContext
    
    AmbientContext --> StorageProvider
    StorageProvider --> AsyncLocal
    StorageProvider --> LogicalCall
    AsyncLocal & LogicalCall --> IStorage
    
    AmbientContext --> ContextFactory
    AmbientContext --> ContextLocator
    
    AmbientContext --> QuerySystem
    QueryProxy --> ConnFactory
    ProxyAsync --> ConnFactory
    
    ConnFactory --> Dapper
    Dapper --> DB

    %% Click Events
    click ConsoleApp "https://github.com/sakopov/Dapper.AmbientContext/tree/master/samples/Dapper.AmbientContext.Examples.ConsoleApp/"
    click WebApp "https://github.com/sakopov/Dapper.AmbientContext/tree/master/samples/Dapper.AmbientContext.Examples.WebApp/"
    click AmbientContext "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContext.cs"
    click AsyncLocal "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/Storage/AsyncLocalContextStorage.cs"
    click LogicalCall "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/Storage/LogicalCallContextStorage.cs"
    click IStorage "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/Storage/IContextualStorage.cs"
    click ConnFactory "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/IDbConnectionFactory.cs"
    click ContextFactory "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContextFactory.cs"
    click ContextLocator "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContextLocator.cs"
    click QueryProxy "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContext.Proxy.cs"
    click ProxyAsync "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContext.ProxyAsync.cs"
    click StorageProvider "https://github.com/sakopov/Dapper.AmbientContext/blob/master/src/Dapper.AmbientContext/AmbientDbContextStorageProvider.cs"

    %% Styling
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef storage fill:#4CAF50,stroke:#2E7D32,color:white
    classDef query fill:#FF9800,stroke:#EF6C00,color:white
    classDef infra fill:#9E9E9E,stroke:#616161,color:white
    classDef app fill:#90A4AE,stroke:#546E7A,color:white
    classDef db fill:#78909C,stroke:#37474F,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Storage Components"]:::storage
        L3["Query Components"]:::query
        L4["Infrastructure"]:::infra
        L5["Applications"]:::app
        L6["Database"]:::db
    end
```
