Class CosmosRepository<T>
Basic implementation of a Cosmos DB repository. Provides basic CRUD operations for a Cosmos DB entity, manages the container instance, and provides common functionality for custom queries as protected values and methods.
Implements
Inherited Members
Namespace: Benday.CosmosDb.Repositories
Assembly: Benday.CosmosDb.dll
Syntax
public abstract class CosmosRepository<T> : IRepository<T> where T : class, ICosmosIdentity, new()
Type Parameters
| Name | Description |
|---|---|
| T | Domain model type managed by this repository |
Constructors
| Edit this page View SourceCosmosRepository(IOptions<CosmosRepositoryOptions<T>>, CosmosClient, ILogger)
Constructor for the repository.
Declaration
public CosmosRepository(IOptions<CosmosRepositoryOptions<T>> options, CosmosClient client, ILogger logger)
Parameters
| Type | Name | Description |
|---|---|---|
| IOptions<CosmosRepositoryOptions<T>> | options | Configuration options |
| CosmosClient | client | Cosmos Db client instance. NOTE: for performance reasons, this should probably be a singleton in the application. |
| ILogger | logger |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException |
Fields
| Edit this page View Source_Options
Declaration
protected readonly CosmosRepositoryOptions<T> _Options
Field Value
| Type | Description |
|---|---|
| CosmosRepositoryOptions<T> |
Properties
| Edit this page View SourceBatchSize
Batch size for saving items to the Cosmos DB container. This is used to limit the number of items saved in a single batch. Default is 50 items per batch.
Declaration
protected virtual int BatchSize { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
DiscriminatorValue
Get the discriminator value for the entity. By default this is the class name for the domain model type managed by this repository.
Declaration
public virtual string DiscriminatorValue { get; }
Property Value
| Type | Description |
|---|---|
| string |
Logger
Declaration
protected ILogger Logger { get; }
Property Value
| Type | Description |
|---|---|
| ILogger |
Methods
| Edit this page View SourceAfterSaveBatch(TransactionalBatchResponse, T[], int, int)
Declaration
protected virtual Task AfterSaveBatch(TransactionalBatchResponse response, T[] batch, int currentBatch, int batchCount)
Parameters
| Type | Name | Description |
|---|---|---|
| TransactionalBatchResponse | response | |
| T[] | batch | |
| int | currentBatch | |
| int | batchCount |
Returns
| Type | Description |
|---|---|
| Task |
BeforeSaveBatch(TransactionalBatch, T[], int, int)
Declaration
protected virtual Task BeforeSaveBatch(TransactionalBatch cosmosBatch, T[] batch, int currentBatch, int batchCount)
Parameters
| Type | Name | Description |
|---|---|---|
| TransactionalBatch | cosmosBatch | |
| T[] | batch | |
| int | currentBatch | |
| int | batchCount |
Returns
| Type | Description |
|---|---|
| Task |
DeleteAsync(string)
Delete an item from the Cosmos DB container.
Declaration
public Task DeleteAsync(string id)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id | Id of the item |
Returns
| Type | Description |
|---|---|
| Task |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
GetAllAsync()
Get all items in the repository. NOTE: this almost certainly performs a cross-partition query and should be used with caution.
Declaration
public Task<IEnumerable<T>> GetAllAsync()
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<T>> | The matching items |
GetByIdAsync(string)
Get an item by its id. This method will return null if the item is not found. NOTE: this almost certainly performs a cross-partition query and should be used with caution because it does not use a partition key.
Declaration
public Task<T?> GetByIdAsync(string id)
Parameters
| Type | Name | Description |
|---|---|---|
| string | id |
Returns
| Type | Description |
|---|---|
| Task<T> | The first matching entity |
GetContainer()
Get the container instance. This method will initialize the container if it is null.
Declaration
protected Task<Container> GetContainer()
Returns
| Type | Description |
|---|---|
| Task<Container> | Reference to the container |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
GetPagedAsync(int, string?)
Gets a page of results with continuation support for efficient large result set retrieval.
Declaration
public virtual Task<PagedResults<T>> GetPagedAsync(int pageSize = 100, string? continuationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| int | pageSize | Maximum number of items to return in this page |
| string | continuationToken | Continuation token from previous query (null for first page) |
Returns
| Type | Description |
|---|---|
| Task<PagedResults<T>> | A page of results with continuation information |
GetPagedAsync(string, int, string?)
Gets a page of results for a specific partition with continuation support.
Declaration
protected virtual Task<PagedResults<T>> GetPagedAsync(string firstLevelPartitionKeyValue, int pageSize = 100, string? continuationToken = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | firstLevelPartitionKeyValue | Value to use for the first-level partition key |
| int | pageSize | Maximum number of items to return in this page |
| string | continuationToken | Continuation token from previous query (null for first page) |
Returns
| Type | Description |
|---|---|
| Task<PagedResults<T>> | A page of results with continuation information |
GetPartitionKey(string, string)
Get the partition key for an item.
Declaration
protected virtual PartitionKey GetPartitionKey(string partitionKey, string discriminatorValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | partitionKey | Top-level partition key value |
| string | discriminatorValue | Second-level partition key value |
Returns
| Type | Description |
|---|---|
| PartitionKey |
GetPartitionKey(T)
Get the partition key for an item.
Declaration
protected virtual PartitionKey GetPartitionKey(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item |
Returns
| Type | Description |
|---|---|
| PartitionKey |
GetQueryDescription(string)
Gets a description for a query. By default, this will return the type name of the repository and the method name. By default, detect and use the method name of the caller.
Declaration
protected string GetQueryDescription(string methodName = "")
Parameters
| Type | Name | Description |
|---|---|---|
| string | methodName | Method that's calling the query |
Returns
| Type | Description |
|---|---|
| string |
GetQueryDescription(string, string)
Gets a description for a query. By default, this will return the type name of the repository and the method name as a formatted string.
Declaration
protected string GetQueryDescription(string typeName, string methodName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | typeName | Name of the type |
| string | methodName | Name of the method |
Returns
| Type | Description |
|---|---|
| string | Formatted query description string |
GetQueryable()
Get the queryable object for the repository. This method will create a queryable object WITHOUT a partition key configuration for the repository.
Declaration
protected virtual Task<QueryableInfo<T>> GetQueryable()
Returns
| Type | Description |
|---|---|
| Task<QueryableInfo<T>> |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
GetQueryable(string)
Creates a queryable for the repository with the specified partition key configuration. This is the starting point for all custom LINQ queries built off of this repository by child repository classes.
Declaration
protected virtual Task<QueryableInfo<T>> GetQueryable(string firstLevelPartitionKeyValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | firstLevelPartitionKeyValue | Value to use for the first-level partition key. NOTE: this is probably ownerId. |
Returns
| Type | Description |
|---|---|
| Task<QueryableInfo<T>> | Queryable and it's configured partition key |
GetQueryable(string, string)
Creates a queryable for the repository with the specified partition key configuration. This is the starting point for all custom LINQ queries built off of this repository by child repository classes.
Declaration
protected virtual Task<QueryableInfo<T>> GetQueryable(string firstLevelPartitionKeyValue, string discriminatorValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | firstLevelPartitionKeyValue | Value to use for the first-level partition key. NOTE: this is probably ownerId. |
| string | discriminatorValue | Discriminator value |
Returns
| Type | Description |
|---|---|
| Task<QueryableInfo<T>> | Queryable and it's configured partition key |
GetResults(FeedIterator<T>, string)
Get results from a query
Declaration
protected Task<List<T>> GetResults(FeedIterator<T> resultSetIterator, string queryDescription)
Parameters
| Type | Name | Description |
|---|---|---|
| FeedIterator<T> | resultSetIterator | Feed iterator to read the results from |
| string | queryDescription | Description of this query for logging |
Returns
| Type | Description |
|---|---|
| Task<List<T>> |
GetResults(IQueryable<T>, string, PartitionKey)
Gets the results from a query.
Declaration
protected Task<List<T>> GetResults(IQueryable<T> query, string queryDescription, PartitionKey partitionKey)
Parameters
| Type | Name | Description |
|---|---|---|
| IQueryable<T> | query | query to run |
| string | queryDescription | logging description for the query |
| PartitionKey | partitionKey | partition key that's configured for this query. NOTE: this is purely to logging purposes |
Returns
| Type | Description |
|---|---|
| Task<List<T>> |
Initialize()
Initializes the repository. This method will create the database and container if they don't already exist.
Declaration
protected Task Initialize()
Returns
| Type | Description |
|---|---|
| Task |
IsCrossPartitionQuery(CosmosDiagnostics)
Attempt to determine if a query is a cross-partition query based on the diagnostics.
Declaration
protected bool IsCrossPartitionQuery(CosmosDiagnostics diagnostics)
Parameters
| Type | Name | Description |
|---|---|---|
| CosmosDiagnostics | diagnostics | Diagnostics for a query response |
Returns
| Type | Description |
|---|---|
| bool | True if it detects a cross-partition query. |
SaveAsync(IList<T>)
Save a list of items to the Cosmos DB container. This method will perform an insert if the item does not exist, otherwise it will perform an update. Items are saved in batches of 50 by default.
Declaration
public virtual Task SaveAsync(IList<T> items)
Parameters
| Type | Name | Description |
|---|---|---|
| IList<T> | items | Items to save |
Returns
| Type | Description |
|---|---|
| Task |
Exceptions
| Type | Condition |
|---|---|
| Exception |
SaveAsync(T)
Save an item to the Cosmos DB container. This method will perform an insert if the item does not exist, otherwise it will perform an update.
Declaration
public virtual Task<T> SaveAsync(T saveThis)
Parameters
| Type | Name | Description |
|---|---|---|
| T | saveThis | The item to save |
Returns
| Type | Description |
|---|---|
| Task<T> |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
SaveBatchAsync(int, int, T[])
Declaration
protected virtual Task<TransactionalBatchResponse> SaveBatchAsync(int batchCount, int currentBatch, T[] batch)
Parameters
| Type | Name | Description |
|---|---|---|
| int | batchCount | |
| int | currentBatch | |
| T[] | batch |
Returns
| Type | Description |
|---|---|
| Task<TransactionalBatchResponse> |