AzureCosmosDBRepository.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Azure.Documents;
  4. using Microsoft.Azure.Documents.Client;
  5. using System.Text;
  6. using HaBookCms.ServiceOptions.Options;
  7. using System.Threading.Tasks;
  8. using System.Net;
  9. using HaBookCms.AzureCosmos.CosmosDB.Interfaces;
  10. using System.Linq;
  11. using System.Reflection;
  12. namespace HaBookCms.AzureCosmos.CosmosDB.Implements
  13. {
  14. public class AzureCosmosDBRepository: IAzureCosmosDBRepository
  15. {
  16. private DocumentClient CosmosClient { get; set; }
  17. private DocumentCollection CosmosCollection { get; set; }
  18. private string _Database { get; set; }
  19. public AzureCosmosDBRepository(AzureCosmosDBOptions options)
  20. {
  21. try {
  22. CosmosClient = CosmosDBClientSingleton.getInstance(options.ConnectionString, options.ConnectionKey).GetCosmosDBClient();
  23. _Database = options.Database;
  24. CosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = _Database });
  25. // _connectionString = options.ConnectionString;
  26. }
  27. catch (DocumentClientException de)
  28. {
  29. Exception baseException = de.GetBaseException();
  30. Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
  31. }
  32. catch (Exception e)
  33. {
  34. Exception baseException = e.GetBaseException();
  35. Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
  36. }
  37. finally
  38. {
  39. Console.WriteLine("End of demo, press any key to exit.");
  40. // Console.ReadKey();
  41. }
  42. }
  43. private async Task InitializeCollection<T>()
  44. {
  45. Type t = typeof(T);
  46. if (CosmosCollection == null ||! CosmosCollection.Id.Equals(t.Name)) {
  47. CosmosCollection = await this.CosmosClient.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(_Database), new DocumentCollection { Id = t.Name });
  48. }
  49. }
  50. public async Task<T> Save<T>(object entity) //where T : object, new()
  51. {
  52. Type t = typeof(T);
  53. await InitializeCollection<T>();
  54. ResourceResponse<Document> doc =
  55. await CosmosClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
  56. return (T)entity;
  57. }
  58. public async Task<T> Update<T>(object entity)
  59. {
  60. Type t = typeof(T);
  61. await InitializeCollection<T>();
  62. ResourceResponse<Document> doc =
  63. await CosmosClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_Database, t.Name), entity);
  64. return (T)entity;
  65. }
  66. public async Task<List<T>> Find<T>(string filedName, string value)
  67. {
  68. Type t = typeof(T);
  69. PropertyInfo[] pis = t.GetProperties();
  70. foreach (PropertyInfo pi in pis)
  71. {
  72. if (pi.Name.Equals(filedName)) {
  73. break;
  74. }
  75. //Console.WriteLine(pi.Name);
  76. }
  77. await InitializeCollection<T>();
  78. FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
  79. return CosmosClient.CreateDocumentQuery<T>(
  80. UriFactory.CreateDocumentCollectionUri(_Database, t.Name), queryOptions)
  81. .Where(f => filedName == value).ToList<T>();
  82. }
  83. }
  84. }