# axelheer/nein-linq architecture

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

- Interactive diagram: https://gitdiagram.com/axelheer/nein-linq
- Repository: https://github.com/axelheer/nein-linq

## Overview

1. **What this repo is**
   - `NeinLinq` is a .NET LINQ-expression rewriting library, not an app.
   - Its job is to take a user-written query, transform the expression tree, then hand the rewritten query back to a LINQ provider such as plain LINQ-to-Objects, Entity Framework 6, or Entity Framework Core.

2. **Solution shape**
   - The solution is split into three production packages:
     - `NeinLinq` — core, provider-agnostic expression/query tooling.
     - `NeinLinq.EntityFramework` — EF6-specific query integration.
     - `NeinLinq.EntityFrameworkCore` — EF Core-specific query integration.
   - Test projects mirror those three packages, plus the core library.

3. **Primary architecture boundary: query wrapping vs query execution**
   - The central pattern is: a `ToXxx(...)` extension returns a specialized `IQueryable`/provider wrapper.
   - That wrapper intercepts query execution, rewrites the expression tree, and then delegates to the original query provider.
   - This keeps the public API small while allowing different transformations to be plugged in.

4. **Core rewrite engine**
   - The `src/NeinLinq` project contains the provider-neutral machinery:
     - `RewriteQueryProvider`, `RewriteQueryable`, `RewriteQueryEnumerable`, `RewriteQueryEnumerator`
     - `RewriteQueryBuilder`, `RewriteQueryRewriter`, `RewriteQueryCleaner`
   - These types form the generic query pipeline used by all flavors.
   - The rewrite step is the key “engine room” where expression trees are traversed and replaced.

5. **Lambda injection subsystem**
   - `InjectLambdaAttribute`, `InjectLambdaMetadata`, `InjectLambdaSignature`, and `InjectableQueryBuilder` implement the “replace a method call with a lambda expression body” feature.
   - `NullsafeQueryBuilder` and `NullsafeQueryRewriter` provide a related transformation that inserts null checks into expressions.
   - `SubstitutionQueryBuilder` and `SubstitutionQueryRewriter` support whole-type / method substitution, useful for swapping provider-specific helpers in tests or alternate runtimes.

6. **Translation helpers for reusable predicates and selectors**
   - `PredicateTranslator` / `PredicateTranslation` and `SelectorTranslator` / `SelectorTranslation` are separate subsystems for expression reuse across related types.
   - These are not query providers themselves; they produce transformed expressions that can then be composed into a query.
   - Architecturally, they sit “above” the rewrite engine and feed it more portable predicates/selectors.

7. **Dynamic query support**
   - `DynamicQuery`, `DynamicQueryable`, `DynamicExpression`, and `DynamicCompare` provide string-driven filtering and ordering.
   - This is a smaller auxiliary path that builds expression trees from runtime field/operator input.
   - It complements, but does not replace, the typed translation and rewrite features.

8. **Caching and binding support**
   - `CachedExpression` and `ObjectCache` reduce repeated compilation/lookup overhead for injected lambdas and reusable expressions.
   - `ParameterBinder` handles parameter replacement/binding inside expression trees.
   - These are implementation support layers that make repeated rewriting practical and efficient.

9. **EF6 integration boundary**
   - `NeinLinq.EntityFramework` adapts the core rewrite pipeline to EF6’s queryable/provider model.
   - The EF6 project contains `RewriteDbQueryProvider`, `RewriteDbQueryable`, `RewriteDbQueryBuilder`, and async-aware enumerable/enumerator types.
   - Its builder variants (`InjectableDbQueryBuilder`, `NullsafeDbQueryBuilder`, `SubstitutionDbQueryBuilder`) mirror the core features but are specialized for EF6 query objects.

10. **EF Core integration boundary**
   - `NeinLinq.EntityFrameworkCore` adapts the same concepts to EF Core’s internal query compilation path.
   - It includes `RewriteQueryCompiler`, `RewriteDbContextOptionsExtension`, and `RewriteDbContextOptionsBuilderExtensions`.
   - The important architectural detail is that EF Core integration is enabled through context options, so the rewrite hook is installed into the DbContext pipeline rather than only wrapping `IQueryable`.

11. **Data flow through the system**
   - A user writes a normal query.
   - They opt into a feature with a wrapper call such as inject/nullsafe/substitution or a provider-specific `ToEntityInjectable` / `ToDbInjectable`.
   - The query expression is captured, rewritten by a `QueryBuilder`/`Rewriter`, optionally cleaned, then executed by the original provider.
   - For EF6/EF Core, the rewritten expression still ends up in the provider’s normal translation/execution path.

12. **Testing layout and verification strategy**
   - Each subsystem has corresponding unit tests in `test/NeinLinq.Tests`.
   - EF6 and EF Core each have both “fake” and “real” tests, indicating two verification layers:
     - lightweight tests for rewrite behavior,
     - integration tests against actual provider behavior.
   - This is a strong sign that the architecture is centered on expression semantics and provider compatibility, not just pure library logic.

13. **How to read this repo as a diagram**
   - Draw one core “expression rewrite engine” box in the middle.
   - Feed it from three feature boxes: injection, null-safety, substitution.
   - Show two adapter boxes on the side: EF6 and EF Core.
   - Show the dynamic query and predicate/selector translation helpers as upstream expression builders that also feed the core engine.
   - The final output is always an `IQueryable` or provider-compatible expression passed back to the underlying LINQ provider.

Main entry points: [`src/NeinLinq`](https://github.com/axelheer/nein-linq/tree/main/src/NeinLinq) (Core API), [`src/NeinLinq/RewriteQueryBuilder.cs`](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryBuilder.cs) (Query builders), [`src/NeinLinq/RewriteQueryProvider.cs`](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryProvider.cs) (Rewrite engine), [`src/NeinLinq.EntityFrameworkCore`](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore) (EF Core package)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core rewrite"]
  node_core_api["Core API"]
  node_core_rewrite["Rewrite engine<br/>pipeline"]
  node_core_builders["Query builders<br/>rewriter"]
  node_core_inject["Lambda inject<br/>feature"]
  node_core_nullsafe["Null-safe rewrite<br/>feature"]
  node_core_subst["Substitution rewrite<br/>feature"]
  node_core_translate["Predicate/selector translation"]
  node_core_dynamic["Dynamic query<br/>expression-builder<br/>[DynamicQuery.cs]"]
  node_core_support(("Support layers"))
end

subgraph group_ef6["EF6 adapter"]
  node_ef6_api["EF6 package<br/>adapter"]
  node_ef6_provider["EF6 provider<br/>provider-adapter"]
  node_ef6_builders["EF6 builders<br/>adapters"]
end

subgraph group_efcore["EF Core adapter"]
  node_efcore_api["EF Core package<br/>adapter"]
  node_efcore_hook["EF Core hook<br/>pipeline-hook"]
  node_efcore_provider["EF Core provider<br/>provider-adapter"]
  node_efcore_builders["EF Core builders<br/>adapters"]
end

subgraph group_tests["Tests"]
  node_tests_core["Core tests<br/>test suite<br/>[NeinLinq.Tests]"]
  node_tests_ef6["EF6 tests<br/>test suite"]
  node_tests_efcore["EF Core tests<br/>test suite"]
end

node_core_api -->|"uses"| node_core_rewrite
node_core_api -->|"exposes"| node_core_translate
node_core_api -->|"exposes"| node_core_dynamic
node_core_api -->|"relies on"| node_core_support
node_core_rewrite -->|"delegates to"| node_core_builders
node_core_builders -->|"implements"| node_core_inject
node_core_builders -->|"implements"| node_core_nullsafe
node_core_builders -->|"implements"| node_core_subst
node_core_translate -->|"feeds"| node_core_rewrite
node_core_dynamic -->|"feeds"| node_core_rewrite
node_core_support -->|"supports"| node_core_builders
node_ef6_api -->|"wraps"| node_ef6_provider
node_ef6_provider -->|"adapts"| node_core_rewrite
node_ef6_api -->|"exposes"| node_ef6_builders
node_ef6_builders -->|"specializes"| node_core_builders
node_efcore_api -->|"installs"| node_efcore_hook
node_efcore_api -->|"wraps"| node_efcore_provider
node_efcore_hook -->|"adapts"| node_core_rewrite
node_efcore_provider -->|"adapts"| node_core_rewrite
node_efcore_api -->|"exposes"| node_efcore_builders
node_efcore_builders -->|"specializes"| node_core_builders
node_tests_core -->|"verifies"| node_core_api
node_tests_ef6 -->|"verifies"| node_ef6_api
node_tests_efcore -->|"verifies"| node_efcore_api
node_tests_ef6 -.->|"covers"| node_core_api
node_tests_efcore -.->|"covers"| node_core_api

click node_core_api "https://github.com/axelheer/nein-linq/tree/main/src/NeinLinq"
click node_core_rewrite "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryProvider.cs"
click node_core_builders "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryBuilder.cs"
click node_core_inject "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/InjectLambdaAttribute.cs"
click node_core_nullsafe "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/NullsafeQueryBuilder.cs"
click node_core_subst "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/SubstitutionQueryBuilder.cs"
click node_core_translate "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/PredicateTranslator.cs"
click node_core_dynamic "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/DynamicQuery.cs"
click node_core_support "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/CachedExpression.cs"
click node_ef6_api "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework"
click node_ef6_provider "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework/RewriteDbQueryProvider.cs"
click node_ef6_builders "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework/RewriteDbQueryBuilder.cs"
click node_efcore_api "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore"
click node_efcore_hook "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteQueryCompiler.cs"
click node_efcore_provider "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryProvider.cs"
click node_efcore_builders "https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryBuilder.cs"
click node_tests_core "https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.Tests"
click node_tests_ef6 "https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.EntityFramework.Tests"
click node_tests_efcore "https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.EntityFrameworkCore.Tests"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_core_api,node_core_rewrite,node_core_builders,node_core_inject,node_core_nullsafe,node_core_subst,node_core_translate,node_core_dynamic,node_core_support toneBlue
class node_ef6_api,node_ef6_provider,node_ef6_builders toneAmber
class node_efcore_api,node_efcore_hook,node_efcore_provider,node_efcore_builders toneMint
class node_tests_core,node_tests_ef6,node_tests_efcore toneRose
```

## Components

### Core rewrite

Provider-neutral expression rewriting, translation, and dynamic query helpers.

- [Core API](https://github.com/axelheer/nein-linq/tree/main/src/NeinLinq): library · Public entry points and reusable expression/query helpers. · `src/NeinLinq`
- [Rewrite engine](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryProvider.cs): pipeline · Generic query wrapping, rewriting, and cleanup pipeline. · `src/NeinLinq/RewriteQueryProvider.cs`
- [Query builders](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/RewriteQueryBuilder.cs): rewriter · Feature-specific builders and rewriters for injectable, nullsafe, and substitution flows. · `src/NeinLinq/RewriteQueryBuilder.cs`
- [Lambda inject](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/InjectLambdaAttribute.cs): feature · Method-call replacement with lambda bodies via injection metadata. · `src/NeinLinq/InjectLambdaAttribute.cs`
- [Null-safe rewrite](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/NullsafeQueryBuilder.cs): feature · Adds null checks during expression rewriting. · `src/NeinLinq/NullsafeQueryBuilder.cs`
- [Substitution rewrite](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/SubstitutionQueryBuilder.cs): feature · Whole-type and method substitution support. · `src/NeinLinq/SubstitutionQueryBuilder.cs`
- [Predicate/selector translation](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/PredicateTranslator.cs): translation · Reusable expression translators for related types. · `src/NeinLinq/PredicateTranslator.cs`
- [Dynamic query](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/DynamicQuery.cs): expression-builder · String-driven filtering and ordering expression builders. · `src/NeinLinq/DynamicQuery.cs`
- [Support layers](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq/CachedExpression.cs): support · Caching and parameter binding used by repeated rewrites. · `src/NeinLinq/CachedExpression.cs`

### EF6 adapter

Integration layer that wraps EF6 queryables and providers.

- [EF6 package](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework): adapter · EF6-specific query integration package. · `src/NeinLinq.EntityFramework`
- [EF6 provider](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework/RewriteDbQueryProvider.cs): provider-adapter · Queryable/provider wrapper that intercepts execution and rewrites expressions. · `src/NeinLinq.EntityFramework/RewriteDbQueryProvider.cs`
- [EF6 builders](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFramework/RewriteDbQueryBuilder.cs): adapters · EF6-specialized injectable, nullsafe, and substitution builders. · `src/NeinLinq.EntityFramework/RewriteDbQueryBuilder.cs`

### EF Core adapter

Integration layer that hooks rewriting into EF Core query compilation and options.

- [EF Core package](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore): adapter · EF Core integration package. · `src/NeinLinq.EntityFrameworkCore`
- [EF Core hook](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteQueryCompiler.cs): pipeline-hook · Installs rewrite behavior through DbContext options and query compiler integration. · `src/NeinLinq.EntityFrameworkCore/RewriteQueryCompiler.cs`
- [EF Core provider](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryProvider.cs): provider-adapter · Queryable/provider wrapper used alongside EF Core query execution. · `src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryProvider.cs`
- [EF Core builders](https://github.com/axelheer/nein-linq/blob/main/src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryBuilder.cs): adapters · EF Core-specialized injectable, nullsafe, and substitution builders. · `src/NeinLinq.EntityFrameworkCore/RewriteEntityQueryBuilder.cs`

### Tests

Unit and integration coverage for core and provider-specific behavior.

- [Core tests](https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.Tests): test suite · Unit tests for rewrite, translation, dynamic query, and support behavior. · `test/NeinLinq.Tests`
- [EF6 tests](https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.EntityFramework.Tests): test suite · Fake and real tests for EF6 integration. · `test/NeinLinq.EntityFramework.Tests`
- [EF Core tests](https://github.com/axelheer/nein-linq/blob/main/test/NeinLinq.EntityFrameworkCore.Tests): test suite · Fake and real tests for EF Core integration. · `test/NeinLinq.EntityFrameworkCore.Tests`

## Connections

- Core API → Rewrite engine: uses · Public query entry points build on the generic rewrite pipeline.
- Core API → Predicate/selector translation: exposes · Translation helpers are part of the reusable public surface.
- Core API → Dynamic query: exposes · Dynamic query helpers build expression trees from runtime input.
- Core API → Support layers: relies on · Caching and binding support expression reuse and performance.
- Rewrite engine → Query builders: delegates to · The core pipeline is parameterized by feature-specific builders.
- Query builders → Lambda inject: implements · Injectable query behavior replaces method calls with lambda bodies.
- Query builders → Null-safe rewrite: implements · Null-safe behavior inserts null checks into expressions.
- Query builders → Substitution rewrite: implements · Substitution behavior swaps methods and types in expressions.
- Predicate/selector translation → Rewrite engine: feeds · Translated predicates and selectors become input to rewriting.
- Dynamic query → Rewrite engine: feeds · Dynamic expressions are rewritten before provider execution.
- Support layers → Query builders: supports · Cached expressions and parameter binding assist rewriting.
- EF6 package → EF6 provider: wraps · The EF6 package centers on a queryable/provider interception layer.
- EF6 provider → Rewrite engine: adapts · EF6 execution is funneled into the provider-neutral rewrite engine.
- EF6 package → EF6 builders: exposes · EF6-specific builder variants mirror the core feature set.
- EF6 builders → Query builders: specializes · EF6 builders reuse the core rewrite patterns for DbQuery objects.
- EF Core package → EF Core hook: installs · EF Core integration is enabled through context options and compiler hooks.
- EF Core package → EF Core provider: wraps · The EF Core package also provides a provider-facing wrapper path.
- EF Core hook → Rewrite engine: adapts · The query compiler hook sends queries through the shared rewrite engine.
- EF Core provider → Rewrite engine: adapts · EF Core query wrappers delegate rewritten expressions to the underlying provider.
- EF Core package → EF Core builders: exposes · EF Core-specialized builders mirror injectable, nullsafe, and substitution flows.
- EF Core builders → Query builders: specializes · EF Core builders are the provider-specific variants of the core builders.
- Core tests → Core API: verifies · Core tests cover rewriting, translation, dynamic queries, and helpers.
- EF6 tests → EF6 package: verifies · EF6 tests exercise fake and real provider behavior.
- EF Core tests → EF Core package: verifies · EF Core tests exercise fake and real provider behavior.
- EF6 tests → Core API: covers · EF6 behavior depends on the shared core engine.
- EF Core tests → Core API: covers · EF Core behavior depends on the shared core engine.
