I am developing an application that using IDocumentClient
to perform query to CosmosDB. My GenericRepository
support for query by Id
and Predicate
.
I am in trouble when change Database from SqlServer to CosmosDb, in CosmosDb, we have partition key
. And I have no idea how to implement repository that support query by partition key
without change interface to pass partition key
as a argument.
public interface IRepository<T>
{
//I can handle this one by adding value of partition key to id and split it by ":"
Task<T> FindByIdAsync(string id);
// I am stuck here!!!
Task<T> FindByPredicateAsync(Expression<Func<T, bool>> predicate);
}
My implementation
public class Repository<T> : IRepository<T>
{
private readonly IDocumentClient _documentClient;
private readonly string _databaseId;
private readonly string _collectionId;
public Repository(IDocumentClient documentClient, string databaseId, string collectionId)
{
_documentClient = documentClient;
_databaseId = databaseId;
_collectionId = collectionId;
}
public async Task<T> FindByIdAsync(string id)
{
var documentUri = UriFactory.CreateDocumentUri(_databaseId, _collectionId, id);
try
{
var result = await _documentClient.ReadDocumentAsync<TDocument>(documentUri, new RequestOptions
{
PartitionKey = ParsePartitionKey(documentId)
});
return result.Document;
}
catch (DocumentClientException e)
{
if (e.StatusCode == HttpStatusCode.NotFound)
{
throw new EntityNotFoundException();
}
throw;
}
}
public async Task<T> FindByPredicateAsync(Expression<Func<T, bool>> predicate)
{
//Need to query CosmosDb with partition key here!
}
private PartitionKey ParsePartitionKey(string entityId) => new PartitionKey(entityId.Split(':')[0]);
}
Any help is greatly appreciated, thanks.