Table of Contents

Class InMemoryDataContext

Namespace
NextORM.Core
Assembly
nextorm.core.dll

In-memory IDataContext implementation that evaluates LINQ queries over Data without a database. Used for unit testing and for querying materialised object graphs with the same operators as the SQL contexts.

public class InMemoryDataContext : IDataContext, IQueryExecutor, IQueryMaterializer, IQueryPlanner, IRowReaderFactory, IQueryCache, IContextEnvironment, IRawCommandExecutor, IAsyncDisposable, IDisposable
Inheritance
InMemoryDataContext
Implements
Inherited Members
Extension Methods

Constructors

InMemoryDataContext()

Creates an empty in-memory context. Seeds the table-alias slot and the plan cache; data is supplied through Data.

public InMemoryDataContext()

Properties

AnyCommand

Lazily created, cached Any query for this context, or null until it is first needed.

public Lazy<QueryCommand<bool>>? AnyCommand { get; set; }

Property Value

Lazy<QueryCommand<bool>>

CacheExpressions

Whether compiled expression delegates are reused between executions. Defaults to false.

public bool CacheExpressions { get; set; }

Property Value

bool

CommandLogger

Logger category used to trace executed commands, or null.

public ILogger? CommandLogger { get; }

Property Value

ILogger

Data

Data source keyed by entity type; add or replace entries to seed queries.

public IDictionary<Type, object?> Data { get; }

Property Value

IDictionary<Type, object>

ExpressionsCache

Per-instance compiled-delegate cache. Deliberately not shared, unlike Metadata and SelectListCache: its entries capture the context instance they were compiled for (see the field comment on _expCache).

public IDictionary<ExpressionKey, Delegate> ExpressionsCache { get; }

Property Value

IDictionary<ExpressionKey, Delegate>

Logger

Logger for the context's diagnostic messages, or null (the in-memory context has no logger factory).

public ILogger? Logger { get; }

Property Value

ILogger

Metadata

Provider-independent entity metadata, shared process-wide with the SQL contexts through DataContextCache. Kept as a member for API compatibility; there is a single source of truth.

public IDictionary<Type, IEntityMetadata> Metadata { get; }

Property Value

IDictionary<Type, IEntityMetadata>

NeedMapping

Whether projected rows must be materialised into CLR objects. Always false for the in-memory context.

public bool NeedMapping { get; }

Property Value

bool

Properties

User-owned bag of arbitrary state attached to this context.

public Dictionary<string, object> Properties { get; }

Property Value

Dictionary<string, object>

QueryCacheEnabled

Whether prepared commands may be stored in and reused from this context's compiled-query cache. Defaults to true; set to false to rebuild every plan (the per-command QueryCommand.Cache flag is not mutated).

public bool QueryCacheEnabled { get; set; }

Property Value

bool

ResultSetEnumeratorLogger

Logger category used by the row-reader path, or null.

public ILogger? ResultSetEnumeratorLogger { get; }

Property Value

ILogger

SelectListCache

Provider-independent entity metadata, shared process-wide with the SQL contexts through DataContextCache. Kept as a member for API compatibility; there is a single source of truth.

public IDictionary<Type, SelectExpression[]> SelectListCache { get; }

Property Value

IDictionary<Type, SelectExpression[]>

Methods

BuildCreateEnumeratorDelegate<TResult>(QueryCommand<TResult>, CancellationToken)

Builds the delegate that creates a row enumerator for a prepared query.

protected CreateEnumeratorDelegate<TResult> BuildCreateEnumeratorDelegate<TResult>(QueryCommand<TResult> queryCommand, CancellationToken cancellationToken)

Parameters

queryCommand QueryCommand<TResult>

The command to build the enumerator for.

cancellationToken CancellationToken

Token used to cancel preparation.

Returns

CreateEnumeratorDelegate<TResult>

A factory that creates enumerators over the command's result.

Type Parameters

TResult

The projected result type.

CreateAsyncEnumerator<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Creates an async enumerator over the prepared command's rows.

public IAsyncEnumerator<TResult> CreateAsyncEnumerator<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to enumerate.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel enumeration.

Returns

IAsyncEnumerator<TResult>

An async enumerator over the result rows.

Type Parameters

TResult

The projected result type.

CreateAsyncEnumerator<TResult>(QueryCommand<TResult>, object[]?, CancellationToken)

Creates an async enumerator for a query command, preparing it if necessary.

protected IAsyncEnumerator<TResult> CreateAsyncEnumerator<TResult>(QueryCommand<TResult> queryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

queryCommand QueryCommand<TResult>

The command to enumerate.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel enumeration.

Returns

IAsyncEnumerator<TResult>

An async enumerator over the result rows.

Type Parameters

TResult

The projected result type.

CreateEnumerator<TResult>(IPreparedQueryCommand<TResult>, object[]?)

Creates a synchronous enumerator over the prepared command's rows.

public IEnumerator<TResult> CreateEnumerator<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to enumerate.

params object[]

Positional parameters bound to the command, or null.

Returns

IEnumerator<TResult>

A synchronous enumerator over the result rows.

Type Parameters

TResult

The projected result type.

Dispose()

Releases the context. Safe to call more than once.

public void Dispose()

Dispose(bool)

Releases managed state. Called from Dispose(); disposing is true for managed cleanup.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true when invoked from Dispose().

DisposeAsync()

Asynchronously releases the context. Delegates to Dispose() so cleanup runs exactly once.

public ValueTask DisposeAsync()

Returns

ValueTask

A completed task.

ExecuteScalar<TResult>(IPreparedQueryCommand<TResult>, object[]?, bool, CancellationToken)

Executes the command and converts the first column of the first row to TResult.

public Task<TResult?> ExecuteScalar<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, bool throwIfNull, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

throwIfNull bool

When true, throws InvalidOperationException when the result is null; otherwise returns default.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<TResult>

The scalar value, or default when the result is null and throwIfNull is false.

Type Parameters

TResult

The scalar type to convert the value to.

ExecuteScalar<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>, bool)

Executes the command and converts the first column of the first row to TResult.

public TResult? ExecuteScalar<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params, bool throwIfNull)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

throwIfNull bool

When true, throws InvalidOperationException when the result is null; otherwise returns default.

Returns

TResult

The scalar value, or default when the result is null and throwIfNull is false.

Type Parameters

TResult

The scalar type to convert the value to.

FirstAsync<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Asynchronously returns the first row of the result.

public Task<TResult> FirstAsync<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<TResult>

The first projected row.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set is empty.

FirstOrDefaultAsync<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Asynchronously returns the first row of the result, or default when the result set is empty.

public Task<TResult?> FirstOrDefaultAsync<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<TResult>

The first projected row, or default when there is none.

Type Parameters

TResult

The projected result type.

FirstOrDefault<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>)

Returns the first row of the result, or default when the result set is empty.

public TResult? FirstOrDefault<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

Returns

TResult

The first projected row, or default when there is none.

Type Parameters

TResult

The projected result type.

First<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>)

Returns the first row of the result.

public TResult First<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

Returns

TResult

The first projected row.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set is empty.

GetEnumerable<TResult>(IPreparedQueryCommand<TResult>, params object[]?)

In-memory override: returns the enumerator directly (it is already IEnumerable<T>), avoiding the compiler-generated yield state machine used by the default interface method.

public IEnumerable<TResult> GetEnumerable<TResult>(IPreparedQueryCommand<TResult> preparedCommand, params object[]? @params)

Parameters

preparedCommand IPreparedQueryCommand<TResult>
params object[]

Returns

IEnumerable<TResult>

Type Parameters

TResult

GetFrom(Type, QueryCommand?)

Returns a FROM expression for srcType; the in-memory context does not need the command's source.

public FromExpression? GetFrom(Type srcType, QueryCommand? queryCommand)

Parameters

srcType Type

The entity or source type to wrap.

queryCommand QueryCommand

Unused; present for interface compatibility.

Returns

FromExpression

A FROM expression over srcType.

GetMap<TResult, TEntity>(QueryCommand<TResult>)

Returns a factory that materialises a TResult from a TEntity row.

public Func<Func<TEntity, TResult>> GetMap<TResult, TEntity>(QueryCommand<TResult> queryCommand)

Parameters

queryCommand QueryCommand<TResult>

The command that defines the projection.

Returns

Func<Func<TEntity, TResult>>

A factory returning a row mapper for the projection.

Type Parameters

TResult

The projected result type.

TEntity

The source entity type.

GetPreparedQueryCommand<TResult>(QueryCommand<TResult>, bool, bool, PreparedCommandCache, CancellationToken)

Prepares queryCommand for in-memory execution with an explicit command-reuse policy, delegating to NextORM.Core.InMemoryQueryBuilder.

public IPreparedQueryCommand<TResult> GetPreparedQueryCommand<TResult>(QueryCommand<TResult> queryCommand, bool createEnumerator, bool storeInCache, PreparedCommandCache cache, CancellationToken cancellationToken)

Parameters

queryCommand QueryCommand<TResult>

The command to prepare.

createEnumerator bool

When true, builds a streaming enumerator as part of preparation.

storeInCache bool

When true (and QueryCacheEnabled is set), stores the prepared command in the plan cache.

cache PreparedCommandCache

The command-reuse policy the prepared command is built with.

cancellationToken CancellationToken

Token used to cancel preparation.

Returns

IPreparedQueryCommand<TResult>

The prepared command, ready to execute.

Type Parameters

TResult

The projected result type.

GetPreparedQueryCommand<TResult>(QueryCommand<TResult>, bool, bool, CancellationToken)

Prepares queryCommand for in-memory execution, delegating to NextORM.Core.InMemoryQueryBuilder.

public IPreparedQueryCommand<TResult> GetPreparedQueryCommand<TResult>(QueryCommand<TResult> queryCommand, bool createEnumerator, bool storeInCache, CancellationToken cancellationToken)

Parameters

queryCommand QueryCommand<TResult>

The command to prepare.

createEnumerator bool

When true, builds a streaming enumerator as part of preparation.

storeInCache bool

When true (and QueryCacheEnabled is set), stores the prepared command in the plan cache.

cancellationToken CancellationToken

Token used to cancel preparation.

Returns

IPreparedQueryCommand<TResult>

The prepared command, ready to execute.

Type Parameters

TResult

The projected result type.

MapColumn(SelectExpression, Expression)

Rewrites column's expression by substituting param for its parameter placeholder.

public Expression MapColumn(SelectExpression column, Expression param)

Parameters

column SelectExpression

The projected column whose expression is accessed.

param Expression

The replacement expression, typically the row or entity parameter.

Returns

Expression

The column expression with the parameter substituted.

PurgeQueryCache()

Clears all cached query plans held by this context.

public void PurgeQueryCache()

ResetPreparation(QueryCommand)

In-memory no-op: there is no compiled plan to invalidate.

public void ResetPreparation(QueryCommand queryCommand)

Parameters

queryCommand QueryCommand

The command to reset.

SingleAsync<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Asynchronously returns the only row of the result.

public Task<TResult> SingleAsync<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<TResult>

The single projected row.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set is empty or contains more than one row.

SingleOrDefaultAsync<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Asynchronously returns the only row of the result, or default when the result set is empty.

public Task<TResult?> SingleOrDefaultAsync<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<TResult>

The single projected row, or default when there is none.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set contains more than one row.

SingleOrDefault<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>)

Returns the only row of the result, or default when the result set is empty.

public TResult? SingleOrDefault<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

Returns

TResult

The single projected row, or default when there is none.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set contains more than one row.

Single<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>)

Returns the only row of the result.

public TResult Single<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

Returns

TResult

The single projected row.

Type Parameters

TResult

The projected result type.

Exceptions

InvalidOperationException

The result set is empty or contains more than one row.

ToListAsync<TResult>(IPreparedQueryCommand<TResult>, object[]?, CancellationToken)

Executes the command and buffers all rows into a list.

public Task<List<TResult>> ToListAsync<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, object[]? @params, CancellationToken cancellationToken)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params object[]

Positional parameters bound to the command, or null.

cancellationToken CancellationToken

Token used to cancel execution.

Returns

Task<List<TResult>>

The materialised rows; empty when the query yields none.

Type Parameters

TResult

The projected result type.

ToList<TResult>(IPreparedQueryCommand<TResult>, ReadOnlySpan<object?>)

Executes the command and buffers all rows into a list.

public List<TResult> ToList<TResult>(IPreparedQueryCommand<TResult> preparedQueryCommand, ReadOnlySpan<object?> @params)

Parameters

preparedQueryCommand IPreparedQueryCommand<TResult>

The prepared command to execute.

params ReadOnlySpan<object>

Positional parameters bound to the command.

Returns

List<TResult>

The materialised rows; empty when the query yields none.

Type Parameters

TResult

The projected result type.