Document Entity Domain Manager


/ Published in: C#
Save to your folder(s)

Document Entity Domain Manager for Azure Mobile Services


Copy this code and paste it in your HTML
  1. using AzureMobileDocDB.Service.DocumentDB;
  2. using Microsoft.Azure.Documents;
  3. using Microsoft.Azure.Documents.Client;
  4. using Microsoft.WindowsAzure.Mobile.Service;
  5. using Microsoft.WindowsAzure.Mobile.Service.Tables;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Net.Http;
  11. using System.Threading.Tasks;
  12. using System.Web;
  13. using System.Web.Http;
  14. using System.Web.Http.OData;
  15. using Microsoft.Azure.Documents.Linq;
  16. using AutoMapper;
  17.  
  18. namespace AzureMobileDocDB.Service
  19. {
  20. public class DocumentEntityDomainManager<TDocument> where TDocument : Resource
  21. {
  22. public HttpRequestMessage Request { get; set; }
  23. public ApiServices Services { get; set; }
  24.  
  25.  
  26. private string _collectionId;
  27. private string _databaseId;
  28. private Database _database;
  29. private DocumentCollection _collection;
  30. private DocumentClient _client;
  31.  
  32. public DocumentEntityDomainManager(string collectionId, string databaseId, HttpRequestMessage request, ApiServices services)
  33. {
  34. Request = request;
  35. Services = services;
  36. _collectionId = collectionId;
  37. _databaseId = databaseId;
  38. }
  39.  
  40. public async Task<bool> DeleteAsync(string id)
  41. {
  42. try
  43. {
  44. var doc = GetDocument(id);
  45.  
  46.  
  47. if (doc == null)
  48. {
  49. return false;
  50. }
  51.  
  52. await Client.DeleteDocumentAsync(doc.SelfLink);
  53.  
  54. return true;
  55.  
  56.  
  57. }
  58. catch (Exception ex)
  59. {
  60. Services.Log.Error(ex);
  61. throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
  62. }
  63. }
  64.  
  65. public async Task<Document> InsertAsync(TDocument data)
  66. {
  67. try
  68. {
  69. return await Client.CreateDocumentAsync(Collection.SelfLink, data);
  70.  
  71.  
  72. }
  73. catch (Exception ex)
  74. {
  75. Services.Log.Error(ex);
  76. throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
  77. }
  78. }
  79.  
  80. public SingleResult<TDocument> Lookup(string id)
  81. {
  82. try
  83. {
  84. return SingleResult.Create<TDocument>(
  85. Client.CreateDocumentQuery<TDocument>(Collection.DocumentsLink)
  86. .Where(d => d.Id == id)
  87. .Select<TDocument, TDocument>(d => d)
  88. );
  89.  
  90.  
  91. }
  92. catch (Exception ex)
  93. {
  94. Services.Log.Error(ex);
  95. throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
  96. }
  97. }
  98.  
  99. public IQueryable<TDocument> Query()
  100. {
  101. try
  102. {
  103. return Client.CreateDocumentQuery<TDocument>(Collection.DocumentsLink);
  104. }
  105. catch (Exception ex)
  106. {
  107. Services.Log.Error(ex);
  108. throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
  109. }
  110. }
  111.  
  112. public async Task<bool> ReplaceAsync(string id, TDocument item)
  113. {
  114.  
  115. if (item == null || id != item.Id)
  116. {
  117. throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
  118. }
  119.  
  120. try
  121. {
  122. var doc = GetDocument(id);
  123.  
  124. if (doc == null)
  125. {
  126. return false;
  127. }
  128.  
  129. await Client.ReplaceDocumentAsync(doc.SelfLink, item);
  130.  
  131. return true;
  132.  
  133. }
  134. catch (Exception ex)
  135. {
  136. Services.Log.Error(ex);
  137. throw new HttpResponseException(System.Net.HttpStatusCode.InternalServerError);
  138. }
  139. }
  140.  
  141. private Document GetDocument(string id)
  142. {
  143. return Client.CreateDocumentQuery<Document>(Collection.DocumentsLink)
  144. .Where(d => d.Id == id)
  145. .AsEnumerable()
  146. .FirstOrDefault();
  147. }
  148.  
  149. #region DocumentDBClient
  150.  
  151. private DocumentClient Client
  152. {
  153. get
  154. {
  155. if (_client == null)
  156. {
  157. string endpoint = ConfigurationManager.AppSettings["endpoint"];
  158. string authKey = ConfigurationManager.AppSettings["authKey"];
  159. Uri endpointUri = new Uri(endpoint);
  160. _client = new DocumentClient(endpointUri, authKey);
  161. }
  162.  
  163. return _client;
  164. }
  165. }
  166.  
  167. private DocumentCollection Collection
  168. {
  169. get
  170. {
  171. if (_collection == null)
  172. {
  173. _collection = ReadOrCreateCollection(Database.SelfLink);
  174. }
  175.  
  176. return _collection;
  177. }
  178. }
  179.  
  180. private Database Database
  181. {
  182. get
  183. {
  184. if (_database == null)
  185. {
  186. _database = ReadOrCreateDatabase();
  187. }
  188.  
  189. return _database;
  190. }
  191. }
  192.  
  193. private DocumentCollection ReadOrCreateCollection(string databaseLink)
  194. {
  195. var col = Client.CreateDocumentCollectionQuery(databaseLink)
  196. .Where(c => c.Id == _collectionId)
  197. .AsEnumerable()
  198. .FirstOrDefault();
  199.  
  200. if (col == null)
  201. {
  202. col = Client.CreateDocumentCollectionAsync(databaseLink, new DocumentCollection { Id = _collectionId }).Result;
  203. }
  204.  
  205. return col;
  206. }
  207.  
  208. private Database ReadOrCreateDatabase()
  209. {
  210. var db = Client.CreateDatabaseQuery()
  211. .Where(d => d.Id == _databaseId)
  212. .AsEnumerable()
  213. .FirstOrDefault();
  214.  
  215. if (db == null)
  216. {
  217. db = Client.CreateDatabaseAsync(new Database { Id = _databaseId }).Result;
  218. }
  219.  
  220. return db;
  221. }
  222. #endregion
  223.  
  224. }
  225. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.