123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Collections.Generic;
- using Microsoft.Azure.Documents;
- using Microsoft.Azure.Documents.Client;
- using System.Text;
- using HaBookCms.ServiceOptions.Options;
- using System.Threading.Tasks;
- using System.Net;
- using HaBookCms.AzureCosmos.CosmosDB.Interfaces;
- using System.Linq;
- using System.Reflection;
- namespace HaBookCms.AzureCosmos.CosmosDB.Implements
- {
- public class AzureCosmosDBRepository: IAzureCosmosDBRepository
- {
- private DocumentClient CosmosClient { get; set; }
- private DocumentCollection CosmosCollection { get; set; }
- private string _Database { get; set; }
- public AzureCosmosDBRepository(AzureCosmosDBOptions options)
- {
- try {
- CosmosClient = CosmosDBClientSingleton.getInstance(options.ConnectionString, options.ConnectionKey).GetCosmosDBClient();
- _Database = options.Database;
- CosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = _Database });
- // _connectionString = options.ConnectionString;
- }
- catch (DocumentClientException de)
- {
- Exception baseException = de.GetBaseException();
- Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
- }
- catch (Exception e)
- {
- Exception baseException = e.GetBaseException();
- Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
- }
- finally
- {
- Console.WriteLine("End of demo, press any key to exit.");
- // Console.ReadKey();
- }
- }
- private async Task InitializeCollection<T>()
- {
- Type t = typeof(T);
- if (CosmosCollection == null ||! CosmosCollection.Id.Equals(t.Name)) {
- CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(_Database), new DocumentCollection { Id = t.Name });
-
- }
- }
- public async Task<T> Save<T>(object entity) //where T : object, new()
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
- return (T)entity;
- }
- public async Task<T> Update<T>(object entity)
- {
- Type t = typeof(T);
- await InitializeCollection<T>();
- ResourceResponse<Document> doc =
- await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
- return (T)entity;
- }
- public async Task<List<T>> Find<T>(string filedName, string value)
- {
- Type t = typeof(T);
- PropertyInfo[] pis = t.GetProperties();
-
- foreach (PropertyInfo pi in pis)
- {
- if (pi.Name.Equals(filedName)) {
-
- break;
- }
- //Console.WriteLine(pi.Name);
- }
- await InitializeCollection<T>();
- FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
-
- return CosmosClient.CreateDocumentQuery<T>(
- UriFactory.CreateDocumentCollectionUri(_Database, t.Name), queryOptions)
- .Where(f => filedName == value).ToList<T>();
-
- }
- }
- }
|