Search documentationEsc

IReadOnlyRepository<TItem>

This is the read-only repository interface for any implementation of

Namespace: Microsoft.Azure.CosmosRepository
Kind: interface

This is the read-only repository interface for any implementation of TItem, exposing asynchronous read functionality.

public interface IReadOnlyRepository<TItem> where TItem : IItem

Type parameters

NameDescription
TItemThe IItem implementation class type.

Example

With DI, use .ctor injection to require any implementation of IItem:

public class ConsumingService
{
readonly IReadOnlyRepository<SomePoco> _pocoRepository;
public ConsumingService(
IReadOnlyRepository<SomePoco> pocoRepository) =>
_pocoRepository = pocoRepository;
}

Members

Methods

IReadOnlyRepository<TItem>.TryGetAsync(string, string?, CancellationToken)

Attempts to get an IItem that corresponds to the given id.

ValueTask<TItem?> TryGetAsync(string id, string? partitionKeyValue = null, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
idStringThe string identifier.
partitionKeyValueStringThe partition key value if different than the Id.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsValueTask{{TItem}}

This implementation handles the case in which a CosmosException with the status code of 404. It will return null when this exception is thrown.

IReadOnlyRepository<TItem>.GetAsync(string, string?, CancellationToken)

Gets the IItem implementation class instance as a TItem that corresponds to the given id.

ValueTask<TItem> GetAsync(string id, string? partitionKeyValue = null, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
idStringThe string identifier.
partitionKeyValueStringThe partition key value if different than the Id.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsValueTask{{TItem}}: A ValueTask representing the IItem implementation class instance as a TItem.

If the typeof(TItem).Name differs from the item.Type you’re attempting to retrieve, null is returned.

IReadOnlyRepository<TItem>.GetAsync(string, PartitionKey, CancellationToken)

Gets the IItem implementation class instance as a TItem that corresponds to the given id.

ValueTask<TItem> GetAsync(string id, PartitionKey partitionKey, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
idStringThe string identifier.
partitionKeyPartitionKeyThe PartitionKey value if different than the Id.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsValueTask{{TItem}}: A ValueTask representing the IItem implementation class instance as a TItem.

If the typeof(TItem).Name differs from the item.Type you’re attempting to retrieve, null is returned.

IReadOnlyRepository<TItem>.GetAsync(Expression<Func<TItem, bool>>, CancellationToken)

Gets an IEnumerable collection of IItem implementation classes that match the given predicate.

ValueTask<IEnumerable<TItem>> GetAsync(Expression<Func<TItem, bool>> predicate, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}The expression used for evaluating a matching item.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsIEnumerable{{TItem}}}: A collection of item instances who meet the predicate condition.

If the typeof(TItem).Name differs from the item.Type you’re attempting to retrieve, the item is not returned.

IReadOnlyRepository<TItem>.GetByQueryAsync(string, CancellationToken)

Gets an IEnumerable collection of IItem by the given Cosmos SQL query

ValueTask<IEnumerable<TItem>> GetByQueryAsync(string query, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
queryStringThe Cosmos SQL query
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsIEnumerable{{TItem}}}: A collection of item instances returned by the given query Cosmos SQL query.

IReadOnlyRepository<TItem>.GetByQueryAsync(QueryDefinition, CancellationToken)

Gets an IEnumerable collection of IItem by the given Cosmos QueryDefinition

ValueTask<IEnumerable<TItem>> GetByQueryAsync(QueryDefinition queryDefinition, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
queryDefinitionQueryDefinition
cancellationTokenCancellationToken

ReturnsIEnumerable{{TItem}}}: A collection of item instances returned by the given queryDefinition Cosmos SQL query.

IReadOnlyRepository<TItem>.ExistsAsync(string, string?, CancellationToken)

Queries cosmos DB to see if an item exists.

ValueTask<bool> ExistsAsync(string id, string? partitionKeyValue = null, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
idStringThe string identifier.
partitionKeyValueStringThe partition key value if different than the Id.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsBoolean}: A ValueTask representing the asynchronous exists operation.

This method performs a point read to decide whether or not an item exists.

IReadOnlyRepository<TItem>.ExistsAsync(string, PartitionKey, CancellationToken)

Queries cosmos DB to see if an item exists.

ValueTask<bool> ExistsAsync(string id, PartitionKey partitionKey, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
idStringThe string identifier.
partitionKeyPartitionKeyThe PartitionKey value if different than the Id.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsBoolean}: A ValueTask representing the asynchronous exists operation.

This method performs a point read to decide whether or not an item exists.

IReadOnlyRepository<TItem>.ExistsAsync(Expression<Func<TItem, bool>>, CancellationToken)

Queries cosmos DB to see if an item exists.

ValueTask<bool> ExistsAsync(Expression<Func<TItem, bool>> predicate, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}The expression used for evaluating any matching items.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsBoolean}: A ValueTask representing the asynchronous exists operation.

This checks the count of the resulting query any count greater than 1 will return true.

IReadOnlyRepository<TItem>.CountAsync(CancellationToken)

Queries cosmos DB to obtain the count of items.

ValueTask<int> CountAsync(CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsInt32}: A ValueTask representing the asynchronous count operation.

This queries the total number of documents in the container.

IReadOnlyRepository<TItem>.CountAsync(Expression<Func<TItem, bool>>, CancellationToken)

Queries cosmos DB to obtain the count of items.

ValueTask<int> CountAsync(Expression<Func<TItem, bool>> predicate, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}The expression used for evaluating any matching items.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsInt32}: A ValueTask representing the asynchronous count operation.

This queries the total number of documents in the container as filtered by the provided predicate.

IReadOnlyRepository<TItem>.PageAsync(Expression<Func<TItem, bool>>?, int, string?, bool, CancellationToken)

Offers a load more paging implementation for infinite scroll scenarios. Allows for efficient paging making use of cosmos DBs continuation tokens, making this implementation cost effective.

ValueTask<IPage<TItem>> PageAsync(Expression<Func<TItem, bool>>? predicate = null, int pageSize = 25, string? continuationToken = null, bool returnTotal = false, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}A filter criteria for the paging operation, if null it will get all IItems
pageSizeInt32The size of the page to return from cosmos db.
continuationTokenStringThe token returned from a previous query, if null starts at the beginning of the data
returnTotalBooleanSpecifies whether or not to return the total number of items that matched the query. This defaults to false as it can be a very expensive operation.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsIPage{{TItem}}}: An IPage of IItems

This method makes use of cosmos dbs continuation tokens for efficient, cost effective paging utilising low RUs

IReadOnlyRepository<TItem>.QueryAsync<TResult>(ISpecification<TItem, TResult>, CancellationToken)

Get items based on a specification. The specification is used to define which filters are used, the order of the search results and how they are paged. Depending on how results are paged derive specification implementations from different classes: For non paged results derive DefaultSpecification For continuation token derive ContinuationTokenSpecification For page number results derive OffsetByPageNumberSpecification

ValueTask<TResult> QueryAsync<TResult>(ISpecification<TItem, TResult> specification, CancellationToken cancellationToken = default) where TResult : IQueryResult<TItem>

Parameters

NameTypeDescription
specificationISpecification{{TItem},{TResult}}A specification used to filtering, ordering and paging. A ISpecification
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsValueTask{{TResult}}: The selected TResult implementation that implements IQueryResult of IItem

This method makes use of cosmos dbs continuation tokens for efficient, cost effective paging utilising low RUs

IReadOnlyRepository<TItem>.PageAsync(Expression<Func<TItem, bool>>?, int, int, bool, CancellationToken)

Offers a load more paging implementation for infinite scroll scenarios. Allows for efficient paging making use of cosmos DBs continuation tokens, making this implementation cost effective.

ValueTask<IPageQueryResult<TItem>> PageAsync(Expression<Func<TItem, bool>>? predicate = null, int pageNumber = 1, int pageSize = 25, bool returnTotal = false, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}A filter criteria for the paging operation, if null it will get all IItems
pageNumberInt32The page number to return from cosmos db.
pageSizeInt32The size of the page to return from cosmos db.
returnTotalBooleanSpecifies whether or not to return the total number of items that matched the query. This defaults to false as it can be a very expensive operation.
cancellationTokenCancellationTokenThe cancellation token to use when making asynchronous operations.

ReturnsIPageQueryResult{{TItem}}}: An IPageQueryResult of IItems

This method makes use of Cosmos DB’s continuation tokens for efficient, cost effective paging utilizing low RUs

IReadOnlyRepository<TItem>.PageAsync(Expression<Func<TItem, bool>>?, int, int, CancellationToken)

Wraps the existing paging support to return an IAsyncEnumerable where T is TItem.

IAsyncEnumerable<TItem> PageAsync(Expression<Func<TItem, bool>>? predicate = null, int limit = 1000, int pageSize = 25, CancellationToken cancellationToken = default)

Parameters

NameTypeDescription
predicateBoolean}}A filter criteria for the paging operation, if null it will get all IItems
limitInt32The limit of how many items to yield. Defaults to 1,000.
pageSizeInt32The size of the page to return from cosmos db.
cancellationTokenCancellationTokenThe optional CancellationToken used to

ReturnsIAsyncEnumerable{{TItem}}: An IAsyncEnumerable where T is TItem.

This method makes use of Cosmos DB’s continuation tokens for efficient, cost effective paging utilizing low RUs