Show / Hide Table of Contents

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.

Inheritance
object
CosmosRepository<T>
CosmosOwnedItemRepository<T>
Implements
IRepository<T>
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
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 Source

CosmosRepository(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 Source

BatchSize

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
| Edit this page View Source

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
| Edit this page View Source

Logger

Declaration
protected ILogger Logger { get; }
Property Value
Type Description
ILogger

Methods

| Edit this page View Source

AfterSaveBatch(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
| Edit this page View Source

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
| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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
| Edit this page View Source

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
| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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>>
| Edit this page View Source

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>>
| Edit this page View Source

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
| Edit this page View Source

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.

| Edit this page View Source

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
| Edit this page View Source

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
| Edit this page View Source

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>

Implements

IRepository<T>
  • Edit this page
  • View Source
In this article
Back to top Copyright © www.benday.com | info@benday.com