Class InMemoryDataContext
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
CacheExpressions
Whether compiled expression delegates are reused between executions. Defaults to false.
public bool CacheExpressions { get; set; }
Property Value
CommandLogger
Logger category used to trace executed commands, or null.
public ILogger? CommandLogger { get; }
Property Value
Data
Data source keyed by entity type; add or replace entries to seed queries.
public IDictionary<Type, object?> Data { get; }
Property Value
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
Logger
Logger for the context's diagnostic messages, or null (the in-memory context has no logger factory).
public ILogger? Logger { get; }
Property Value
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
NeedMapping
Whether projected rows must be materialised into CLR objects. Always false for the in-memory context.
public bool NeedMapping { get; }
Property Value
Properties
User-owned bag of arbitrary state attached to this context.
public Dictionary<string, object> Properties { get; }
Property Value
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
ResultSetEnumeratorLogger
Logger category used by the row-reader path, or null.
public ILogger? ResultSetEnumeratorLogger { get; }
Property Value
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
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
queryCommandQueryCommand<TResult>The command to build the enumerator for.
cancellationTokenCancellationTokenToken used to cancel preparation.
Returns
- CreateEnumeratorDelegate<TResult>
A factory that creates enumerators over the command's result.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to enumerate.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel enumeration.
Returns
- IAsyncEnumerator<TResult>
An async enumerator over the result rows.
Type Parameters
TResultThe 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
queryCommandQueryCommand<TResult>The command to enumerate.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel enumeration.
Returns
- IAsyncEnumerator<TResult>
An async enumerator over the result rows.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to enumerate.
paramsobject[]Positional parameters bound to the command, or null.
Returns
- IEnumerator<TResult>
A synchronous enumerator over the result rows.
Type Parameters
TResultThe projected result type.
Dispose()
Releases the context. Safe to call more than once.
public void Dispose()
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
throwIfNullboolWhen true, throws InvalidOperationException when the result is null; otherwise returns default.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
throwIfNullboolWhen true, throws InvalidOperationException when the result is null; otherwise returns default.
Returns
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
- Task<TResult>
The first projected row.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
Returns
- TResult
The first projected row, or default when there is none.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
Returns
- TResult
The first projected row.
Type Parameters
TResultThe 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
preparedCommandIPreparedQueryCommand<TResult>paramsobject[]
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
srcTypeTypeThe entity or source type to wrap.
queryCommandQueryCommandUnused; 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
queryCommandQueryCommand<TResult>The command that defines the projection.
Returns
Type Parameters
TResultThe projected result type.
TEntityThe 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
queryCommandQueryCommand<TResult>The command to prepare.
createEnumeratorboolWhen true, builds a streaming enumerator as part of preparation.
storeInCacheboolWhen true (and QueryCacheEnabled is set), stores the prepared command in the plan cache.
cachePreparedCommandCacheThe command-reuse policy the prepared command is built with.
cancellationTokenCancellationTokenToken used to cancel preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command, ready to execute.
Type Parameters
TResultThe 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
queryCommandQueryCommand<TResult>The command to prepare.
createEnumeratorboolWhen true, builds a streaming enumerator as part of preparation.
storeInCacheboolWhen true (and QueryCacheEnabled is set), stores the prepared command in the plan cache.
cancellationTokenCancellationTokenToken used to cancel preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command, ready to execute.
Type Parameters
TResultThe 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
columnSelectExpressionThe projected column whose expression is accessed.
paramExpressionThe 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
queryCommandQueryCommandThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
- Task<TResult>
The single projected row.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
Returns
- TResult
The single projected row, or default when there is none.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
Returns
- TResult
The single projected row.
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsobject[]Positional parameters bound to the command, or null.
cancellationTokenCancellationTokenToken used to cancel execution.
Returns
Type Parameters
TResultThe 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
preparedQueryCommandIPreparedQueryCommand<TResult>The prepared command to execute.
paramsReadOnlySpan<object>Positional parameters bound to the command.
Returns
- List<TResult>
The materialised rows; empty when the query yields none.
Type Parameters
TResultThe projected result type.