RunicQueryableRowSource<TRow, TKey> class

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public sealed class RunicQueryableRowSource<TRow, TKey> : IRunicRowSource<TRow, TKey> where TKey : notnull

Inherits or implements IRunicRowSource.

A IRunicRowSource over an IQueryable: an in-memory list (rows.AsQueryable()), or a database table through a LINQ provider such as Entity Framework Core.

Type parameters

TRow

The row type.

TKey

The row key.

Parameters

rows

Returns the rows to query.

fields

The fields the page may sort and filter by.

key

The row key, which the source sorts by last.

Remarks

Each call takes a new query from rows, so a factory can open a short-lived database context per call. Queries are mapped by RunicQueryFields and ordered by the row key last. The source runs the LINQ query synchronously on the caller's thread, which the collection never makes a model turn. A source with asynchronous LINQ (ToListAsync) implements IRunicRowSource itself with the same fields.

RunicQueryableRowSource.IndexOfAsync reads the keys of the filtered, ordered rows until it finds the key. A database source that needs a faster search implements it with ROW_NUMBER().

Cancellation. A load and a key search stop at the next row they read once cancelled, and free their slot of the collection. A synchronous LINQ provider cannot interrupt one call that has not returned a row yet: a Count(), or the database's work before a query's first row. Such a call finishes in the background after its query changed, still holding its slot, so a slow table keeps at most RunicQueryCollectionOptions.MaximumConcurrentLoads of them running. A source with asynchronous LINQ (CountAsync, ToListAsync with the token) is interrupted at once.

Constructors

RunicQueryableRowSource

public RunicQueryableRowSource(Func<IQueryable<TRow>> rows, RunicQueryFields<TRow> fields, Expression<Func<TRow, TKey>> key)

A IRunicRowSource over an IQueryable: an in-memory list (rows.AsQueryable()), or a database table through a LINQ provider such as Entity Framework Core.

Type parameters

TRow

The row type.

TKey

The row key.

Parameters

rows

Returns the rows to query.

fields

The fields the page may sort and filter by.

key

The row key, which the source sorts by last.

Remarks

Each call takes a new query from rows, so a factory can open a short-lived database context per call. Queries are mapped by RunicQueryFields and ordered by the row key last. The source runs the LINQ query synchronously on the caller's thread, which the collection never makes a model turn. A source with asynchronous LINQ (ToListAsync) implements IRunicRowSource itself with the same fields.

RunicQueryableRowSource.IndexOfAsync reads the keys of the filtered, ordered rows until it finds the key. A database source that needs a faster search implements it with ROW_NUMBER().

Cancellation. A load and a key search stop at the next row they read once cancelled, and free their slot of the collection. A synchronous LINQ provider cannot interrupt one call that has not returned a row yet: a Count(), or the database's work before a query's first row. Such a call finishes in the background after its query changed, still holding its slot, so a slow table keeps at most RunicQueryCollectionOptions.MaximumConcurrentLoads of them running. A source with asynchronous LINQ (CountAsync, ToListAsync with the token) is interrupted at once.

Methods

CountAsync

public ValueTask<int> CountAsync(RunicQuery query, CancellationToken cancellationToken)

Counts the rows that match query's filters.

Parameters

query

The query.

cancellationToken

Cancels the count.

Documentation from IRunicRowSource.CountAsync.

IndexOfAsync

public ValueTask<int> IndexOfAsync(RunicQuery query, TKey key, CancellationToken cancellationToken)

Returns the position of the row with key in query's order, or -1.

Parameters

query

The query.

key

The row's key.

cancellationToken

Cancels the search.

Documentation from IRunicRowSource.IndexOfAsync.

LoadAsync

public ValueTask<RunicRowPage<TRow>> LoadAsync(RunicQuery query, RunicRowRange range, CancellationToken cancellationToken)

Loads the rows of range in query's order.

Parameters

query

The query.

range

The rows to load. Fewer rows come back at the end of the list.

cancellationToken

Cancels the load.

Returns

The rows, and optionally the number of rows that match the query, such as from COUNT(*) OVER ().

Documentation from IRunicRowSource.LoadAsync.

Validate

public void Validate(RunicQuery query)

Checks that the source can run query before the collection takes it.

Parameters

query

The query.

Exceptions

ArgumentException

The query names a field or filter the source does not have, or a value it cannot use. The page receives invalid-input, and the collection keeps its query.

Remarks

The query comes from a page and is untrusted: its field names, custom filter names and values are whatever the page sent. Reject every field, operator and value the source does not support by name, rather than passing them to a database. RunicQueryFields.Validate does so for registered fields.

Documentation from IRunicRowSource.Validate.