# stdray/linq2db architecture

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

- Interactive diagram: https://gitdiagram.com/stdray/linq2db
- Repository: https://github.com/stdray/linq2db

## Overview

##### Project Type and Purpose
- This is an open-source .NET data-access library (“LINQ to DB”) that sits between your POCO objects and various relational databases.
- It implements its own LINQ expression parser, SQL AST, mapping engine, and per-database providers to generate and execute SQL efficiently.
- Key use cases: compile-time checked queries, bulk operations, per-DB optimizations, extensibility for new providers.

##### High-Level Components
a. Core API
- DataConnection / DataContext – entry point for users’ queries and operations
- AsyncExtensions – adds async support
- CompiledQuery – pre-compile query expressions
b. Mapping & Metadata
- MappingSchema, EntityDescriptor, ColumnDescriptor – maps POCOs ↔ tables/columns
- Attribute and fluent mapping builders
- MetadataReader implementations – read mapping attributes or XML
c. LINQ Engine
- Linq.Builder – walk expression trees, build SqlQuery AST
- ExpressionQuery – represents LINQ query in internal form
- SqlQuery classes – AST nodes for SELECT, INSERT, UPDATE, DELETE
d. SQL Generation & Optimization
- SqlProvider (BasicSqlBuilder/Optimizer) interfaces
- Per-DB SqlBuilder, SqlOptimizer, Tools – SQL text generation & tweaks
e. Data Providers
- IDataProvider / IDataProviderFactory – abstraction over ADO.NET
- Per-DB subfolders: SqlServer, MySql, PostgreSQL, Oracle, SQLite, Firebird, DB2, Informix, Sybase, SapHana, SqlCe, Access…
- BulkCopy implementations – high-speed inserts, with fallback to batching
f. Schema Providers
- ISchemaProvider – introspect database schema at runtime
- TableSchema, ColumnSchema, ForeignKeySchema, etc.
g. ServiceModel (optional)
- LinqService / LinqSoapService – remote data-service wrappers
h. Utilities & Extensions
- Common tools, conversion helpers, reflection-based object factory
- Transaction support via DataConnection and .NET TransactionScope
i. Tests
- Unit and integration tests per module and per provider (C#, F#, VB)

##### Component Interactions and Data Flow
1) User code calls DataConnection (e.g. new DbNorthwind())
2) DataConnection uses MappingSchema + MetadataReader to map POCOs
3) LINQ expression is parsed by Linq.Builder → produces SqlQuery AST
4) SqlProvider (per-DB) takes AST → generates SQL text, applies optimizations
5) IDataProvider executes SQL via ADO.NET connection to target database
6) Results mapped back to POCOs via MappingSchema + ObjectFactory
7) BulkCopy and transaction calls bypass LINQ engine and use provider-specific APIs

##### Architectural Patterns & Design Principles
- Plugin/Provider pattern: IDataProviderFactory for per-DB implementation
- Expression tree visitor (Interpreter pattern) to translate LINQ → AST → SQL
- Separation of concerns: mapping, query building, SQL generation, execution
- No change-tracking (explicit unit-of-work via DataConnection + transactions)
- Async support via extension methods

##### Detail Level
- Major sub-modules (Mapping, Linq, DataProvider, SchemaProvider)
- How the plugin architecture allows swapping or adding providers
- Don’t list every single file—group by folder/module

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% API Layer
    subgraph "API Layer"
        DC["DataConnection / DataContext"]:::core
        AE["AsyncExtensions"]:::core
        CQ["CompiledQuery"]:::core
    end

    %% Mapping Layer
    subgraph "Mapping & Metadata Layer"
        MS["MappingSchema"]:::core
        ED["EntityDescriptor"]:::core
        CD["ColumnDescriptor"]:::core
        FMB["FluentMappingBuilder"]:::core
        PMB["PropertyMappingBuilder"]:::core
        AR["AttributeReader"]:::core
        XM["XmlAttributeReader"]:::core
        FR["FluentMetadataReader"]:::core
        SR["SystemDataLinqAttributeReader"]:::core
    end

    %% LINQ → AST Layer
    subgraph "LINQ Engine Layer"
        LB["Linq.Builder"]:::core
        EQ["ExpressionQuery"]:::core
        SQ["SqlQuery AST"]:::core
    end

    %% SQL Engine Layer
    subgraph "SQL Generation & Optimization Layer"
        SB["SqlProvider / SqlBuilder"]:::core
        SO["SqlOptimizer"]:::core
        VC["ValueToSqlConverter"]:::core
    end

    %% Provider Layer
    subgraph "Data Providers Layer"
        DP["IDataProvider / IDataProviderFactory"]:::provider
        PB["Per-DB Providers"]:::provider
        BC["BulkCopy"]:::provider
    end

    %% Schema Provider Layer
    subgraph "Schema Providers Layer"
        ISP["ISchemaProvider"]:::core
        TS["TableSchema"]:::core
        CS["ColumnSchema"]:::core
        FKS["ForeignKeySchema"]:::core
    end

    %% ServiceModel
    subgraph "ServiceModel (Optional)"
        LS["LinqService"]:::core
        LSS["LinqSoapService"]:::core
    end

    %% Utilities
    subgraph "Utilities & Extensions"
        CE["Common Extensions"]:::core
        OF["ObjectFactory"]:::core
    end

    %% External Layer
    subgraph "External Systems"
        ADO["ADO.NET Drivers"]:::external
        RDBMS["Relational Databases"]:::external
    end

    %% Connections
    %% API to Mapping
    DC -->|"uses"| MS
    DC -->|"uses"| AR
    DC -->|"uses"| XM
    DC -->|"uses"| FR
    DC -->|"uses"| SR

    AE -->|extends| DC
    CQ -->|pre-compiles| LB

    %% Mapping to LINQ
    MS -->|"provides metadata"| LB

    %% LINQ to SQL AST
    LB -->|"builds AST"| SQ
    EQ -->|"represents query"| SQ

    %% SQL Generation
    SQ -->|"input"| SB
    SB -->|"optimizes"| SO
    SB -->|"converts values"| VC

    %% SQL to Provider
    SB -->|"generates SQL"| DP
    DP -->|"executes via"| ADO
    BC -->|"bulk insert"| DP

    %% Provider to Schema
    DP -->|"reads schema"| ISP
    ISP --> TS
    ISP --> CS
    ISP --> FKS

    %% Provider to DB
    DP -->|"connects"| RDBMS

    %% ServiceModel
    DC --> LS
    DC --> LSS
    LS --> DP
    LSS --> DP

    %% Utilities
    DC --> OF
    DC --> CE

    %% Click Events
    click DC "https://github.com/stdray/linq2db/blob/master/Source/Data/DataConnection.cs"
    click AE "https://github.com/stdray/linq2db/blob/master/Source/AsyncExtensions.cs"
    click CQ "https://github.com/stdray/linq2db/blob/master/Source/CompiledQuery.cs"
    click MS "https://github.com/stdray/linq2db/blob/master/Source/Mapping/MappingSchema.cs"
    click ED "https://github.com/stdray/linq2db/blob/master/Source/Mapping/EntityDescriptor.cs"
    click CD "https://github.com/stdray/linq2db/blob/master/Source/Mapping/ColumnDescriptor.cs"
    click FMB "https://github.com/stdray/linq2db/blob/master/Source/Mapping/FluentMappingBuilder.cs"
    click PMB "https://github.com/stdray/linq2db/blob/master/Source/Mapping/PropertyMappingBuilder.cs"
    click AR "https://github.com/stdray/linq2db/blob/master/Source/Metadata/AttributeReader.cs"
    click XM "https://github.com/stdray/linq2db/blob/master/Source/Metadata/XmlAttributeReader.cs"
    click FR "https://github.com/stdray/linq2db/blob/master/Source/Metadata/FluentMetadataReader.cs"
    click SR "https://github.com/stdray/linq2db/blob/master/Source/Metadata/SystemDataLinqAttributeReader.cs"
    click LB "https://github.com/stdray/linq2db/tree/master/Source/Linq/Builder/"
    click EQ "https://github.com/stdray/linq2db/blob/master/Source/Linq/ExpressionQuery.cs"
    click SQ "https://github.com/stdray/linq2db/blob/master/Source/SqlQuery/SelectQuery.cs"
    click SB "https://github.com/stdray/linq2db/blob/master/Source/SqlProvider/BasicSqlBuilder.cs"
    click SO "https://github.com/stdray/linq2db/blob/master/Source/SqlProvider/BasicSqlOptimizer.cs"
    click VC "https://github.com/stdray/linq2db/blob/master/Source/SqlProvider/ValueToSqlConverter.cs"
    click DP "https://github.com/stdray/linq2db/blob/master/Source/DataProvider/IDataProvider.cs"
    click BC "https://github.com/stdray/linq2db/blob/master/Source/DataProvider/BasicBulkCopy.cs"
    click ISP "https://github.com/stdray/linq2db/blob/master/Source/SchemaProvider/ISchemaProvider.cs"
    click TS "https://github.com/stdray/linq2db/blob/master/Source/SchemaProvider/TableSchema.cs"
    click CS "https://github.com/stdray/linq2db/blob/master/Source/SchemaProvider/ColumnSchema.cs"
    click FKS "https://github.com/stdray/linq2db/blob/master/Source/SchemaProvider/ForeignKeySchema.cs"

    %% Styles
    classDef core fill:#cfe2f3,stroke:#03689a,stroke-width:1px
    classDef provider fill:#d9ead3,stroke:#38761d,stroke-width:1px
    classDef external fill:#fce5cd,stroke:#a64d79,stroke-width:1px
```
