DocumentDB Controller


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

API Controller for Azure Mobile Services with DocumentDB as the backend.


Copy this code and paste it in your HTML
  1. using AzureMobileDocDB.Service;
  2. using AzureMobileDocDB.Service.DocumentDB;
  3. using Microsoft.Azure.Documents;
  4. using Microsoft.WindowsAzure.Mobile.Service;
  5. using Microsoft.WindowsAzure.Mobile.Service.Tables;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  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.  
  16. namespace AzureMobileDocDB.Controllers
  17. {
  18. public abstract class DocumentController<TDocument>: ApiController where TDocument:Resource
  19. {
  20. public ApiServices Services { get; set; }
  21.  
  22. private DocumentEntityDomainManager<TDocument> domainManager;
  23. /// <summary>
  24. /// Gets or sets the <see cref="T:Microsoft.WindowsAzure.Mobile.Service.Tables.IDomainManager`1" /> to be used for accessing the backend store.
  25. /// </summary>
  26. protected DocumentEntityDomainManager<TDocument> DomainManager
  27. {
  28. get
  29. {
  30. if (this.domainManager == null)
  31. {
  32. throw new InvalidOperationException("Domain manager not set");
  33. }
  34. return this.domainManager;
  35. }
  36. set
  37. {
  38. if (value == null)
  39. {
  40. throw new ArgumentNullException("value");
  41. }
  42. this.domainManager = value;
  43. }
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="T:Microsoft.WindowsAzure.Mobile.Service.TableController`1" /> class.
  47. /// </summary>
  48. protected DocumentController()
  49. {
  50. }
  51.  
  52.  
  53. protected virtual IQueryable<TDocument> Query()
  54. {
  55. IQueryable<TDocument> result;
  56. try
  57. {
  58. result = this.DomainManager.Query();
  59. }
  60. catch (HttpResponseException exception)
  61. {
  62. Services.Log.Error(exception, base.Request, LogCategories.Controllers);
  63. throw;
  64. }
  65. catch (Exception ex)
  66. {
  67. Services.Log.Error(ex, base.Request, LogCategories.Controllers);
  68. throw new HttpResponseException( HttpStatusCode.InternalServerError);
  69. }
  70. return result;
  71. }
  72.  
  73. protected virtual SingleResult<TDocument> Lookup(string id)
  74. {
  75.  
  76. try
  77. {
  78. return this.DomainManager.Lookup(id);
  79. }
  80. catch (HttpResponseException exception)
  81. {
  82. Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
  83. throw;
  84. }
  85. catch (Exception ex)
  86. {
  87. Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
  88. throw new HttpResponseException(HttpStatusCode.InternalServerError);
  89. }
  90.  
  91. }
  92.  
  93. protected async virtual Task<Document> InsertAsync(TDocument item)
  94. {
  95. if (item == null)
  96. {
  97. throw new HttpResponseException(HttpStatusCode.BadRequest);
  98. }
  99.  
  100.  
  101. try
  102. {
  103. return await this.DomainManager.InsertAsync(item);
  104.  
  105. }
  106. catch (HttpResponseException exception)
  107. {
  108. Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
  109. throw;
  110. }
  111. catch (Exception ex)
  112. {
  113. Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
  114.  
  115. throw new HttpResponseException(HttpStatusCode.InternalServerError);
  116. }
  117.  
  118. }
  119.  
  120. protected async virtual Task<TDocument> ReplaceAsync(string id, TDocument item)
  121. {
  122. if (item == null || !base.ModelState.IsValid)
  123. {
  124. throw new HttpResponseException(HttpStatusCode.BadRequest);
  125. }
  126.  
  127. TDocument result;
  128.  
  129. try
  130. {
  131. var flag = await this.DomainManager.ReplaceAsync(id, item);
  132.  
  133. if (!flag)
  134. {
  135. throw new HttpResponseException(HttpStatusCode.NotFound);
  136. }
  137.  
  138. result = item;
  139. }
  140. catch (HttpResponseException exception)
  141. {
  142. Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
  143. throw;
  144. }
  145. catch (Exception ex)
  146. {
  147. Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
  148. throw new HttpResponseException(HttpStatusCode.InternalServerError);
  149. }
  150. return result;
  151. }
  152.  
  153. protected virtual async Task DeleteAsync(string id)
  154. {
  155. bool flag = false;
  156. try
  157. {
  158. flag = await this.DomainManager.DeleteAsync(id);
  159.  
  160. }
  161. catch (HttpResponseException exception)
  162. {
  163. Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
  164. throw;
  165. }
  166. catch (Exception ex)
  167. {
  168. Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
  169.  
  170. throw new HttpResponseException( HttpStatusCode.InternalServerError);
  171. }
  172. if (!flag)
  173. {
  174. Services.Log.Warn("Resource not found", base.Request);
  175. throw new HttpResponseException(HttpStatusCode.NotFound);
  176. }
  177.  
  178.  
  179. }
  180.  
  181. }
  182. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.