You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassCosmosDocumentRepository<T>:ICosmosDocumentRepository<T>whereT:BaseDocumentModel{privateDocumentClient_client;privateUri_databaseUri;privateUri_collectionUri;privatereadonlybool_isPartitioned;publicstringDatabaseId{get;privateset;}publicstringCollectionName{get;privateset;}publicCosmosDocumentRepository(stringurl,stringauthKey,stringdatabaseId,stringcollectionName){_databaseUri=UriFactory.CreateDatabaseUri(databaseId);_collectionUri=UriFactory.CreateDocumentCollectionUri(databaseId,collectionName);DatabaseId=databaseId;CollectionName=collectionName;_client=newDocumentClient(newUri(url),authKey);_client.CreateDatabaseIfNotExistsAsync(newDatabase{Id=databaseId});vartypeProperties=typeof(T).GetProperties(BindingFlags.Public|BindingFlags.Instance);varparitionByProperty=typeProperties.FirstOrDefault(p =>p.GetCustomAttributes(typeof(PartitionAttribute),false).FirstOrDefault()!=null);DocumentCollectiondocumentCollection=newDocumentCollection{Id=collectionName};if(paritionByProperty!=null){varpartitionKeyDefinitions=newPartitionKeyDefinition();partitionKeyDefinitions.Paths.Add(paritionByProperty.GetCustomAttribute<PartitionAttribute>().PartitionKeyPath);documentCollection.PartitionKey=partitionKeyDefinitions;_isPartitioned=true;}varresponse=_client.CreateDocumentCollectionIfNotExistsAsync(databaseUri:_databaseUri,documentCollection:documentCollection,options:newRequestOptions{OfferThroughput=25000}).Result;}publicasyncTask<T>UpsertAsync(Titem){RequestOptionsrequestOptions=GetRequestOptions(item);Documentdocument=await_client.UpsertDocumentAsync(_collectionUri,item,requestOptions);return(T)(dynamic)document;}publicasyncTask<T>CreateAsync(Titem){RequestOptionsrequestOptions=GetRequestOptions(item);Documentdocument=await_client.CreateDocumentAsync(_collectionUri,item,requestOptions);return(T)(dynamic)document;}publicasyncTask<T>DeleteAsync(Titem){RequestOptionsrequestOptions=GetRequestOptions(item);Documentdocument=await_client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId,CollectionName,item.Id),requestOptions);return(T)(dynamic)document;}publicasyncTask<T>FindById(stringid){if(_isPartitioned){thrownewException("Paritioned collection needs partition key");}varresults=awaitFindAsync(mds =>mds.Id==id);returnresults.FirstOrDefault();}publicasyncTask<IList<T>>FindByPartition(objectpartitionValue){if(!_isPartitioned){thrownewException("Not a partitioned collection");}FeedOptionsoptions=newFeedOptions{PartitionKey=newPartitionKey(partitionValue),MaxItemCount=1000};IDocumentQuery<T>query=_client.CreateDocumentQuery<T>(_collectionUri,options).AsDocumentQuery();List<T>results=newList<T>();while(query.HasMoreResults){FeedResponse<T>queryResponse=awaitquery.ExecuteNextAsync<T>();Console.WriteLine("Query batch consumed {0} request units",queryResponse.RequestCharge);results.AddRange(queryResponse);}returnresults;}publicTask<T>FindAsync(Titem){returnFindById(item.Id);}//TODO: Pagination & enforcement of Parition in request options.privateasyncTask<IList<T>>FindAsync(Expression<Func<T,bool>>predicate){IDocumentQuery<T>query=_client.CreateDocumentQuery<T>(_collectionUri).Where(predicate).AsDocumentQuery();List<T>results=newList<T>();while(query.HasMoreResults){results.AddRange(awaitquery.ExecuteNextAsync<T>());}returnresults;}/// <summary>/// Partition key is used to identify the target partition for this request. It must be set on read /// and delete operations for all document requests; /// create, read, update and delete operations for all document attachment requests; and execute /// operation on stored producedures./// For create and update operations on documents, the partition key is optional. When absent, the /// client library will extract the partition key from the document before sending the request to the server./// </summary>/// <param name="item">Entity </param>/// <returns></returns>privateRequestOptionsGetRequestOptions(Titem){IPartitionedpartitioned=itemasIPartitioned;RequestOptionsrequestOptions=null;if(partitioned!=null){requestOptions=newRequestOptions{PartitionKey=newPartitionKey(partitioned.PartitionKey)};}returnrequestOptions;}}