How stdray/linq2db works
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