/ Published in: C#
                    
                                        
API Controller for Azure Mobile Services with DocumentDB as the backend.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
using AzureMobileDocDB.Service;
using AzureMobileDocDB.Service.DocumentDB;
using Microsoft.Azure.Documents;
using Microsoft.WindowsAzure.Mobile.Service;
using Microsoft.WindowsAzure.Mobile.Service.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData;
namespace AzureMobileDocDB.Controllers
{
public abstract class DocumentController<TDocument>: ApiController where TDocument:Resource
{
public ApiServices Services { get; set; }
private DocumentEntityDomainManager<TDocument> domainManager;
/// <summary>
/// Gets or sets the <see cref="T:Microsoft.WindowsAzure.Mobile.Service.Tables.IDomainManager`1" /> to be used for accessing the backend store.
/// </summary>
protected DocumentEntityDomainManager<TDocument> DomainManager
{
get
{
if (this.domainManager == null)
{
}
return this.domainManager;
}
set
{
if (value == null)
{
}
this.domainManager = value;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:Microsoft.WindowsAzure.Mobile.Service.TableController`1" /> class.
/// </summary>
protected DocumentController()
{
}
protected virtual IQueryable<TDocument> Query()
{
IQueryable<TDocument> result;
try
{
result = this.DomainManager.Query();
}
catch (HttpResponseException exception)
{
Services.Log.Error(exception, base.Request, LogCategories.Controllers);
throw;
}
catch (Exception ex)
{
Services.Log.Error(ex, base.Request, LogCategories.Controllers);
}
return result;
}
protected virtual SingleResult<TDocument> Lookup(string id)
{
try
{
return this.DomainManager.Lookup(id);
}
catch (HttpResponseException exception)
{
Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
throw;
}
catch (Exception ex)
{
Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
}
}
protected async virtual Task<Document> InsertAsync(TDocument item)
{
if (item == null)
{
}
try
{
return await this.DomainManager.InsertAsync(item);
}
catch (HttpResponseException exception)
{
Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
throw;
}
catch (Exception ex)
{
Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
}
}
protected async virtual Task<TDocument> ReplaceAsync(string id, TDocument item)
{
if (item == null || !base.ModelState.IsValid)
{
}
TDocument result;
try
{
var flag = await this.DomainManager.ReplaceAsync(id, item);
if (!flag)
{
}
result = item;
}
catch (HttpResponseException exception)
{
Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
throw;
}
catch (Exception ex)
{
Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
}
return result;
}
protected virtual async Task DeleteAsync(string id)
{
bool flag = false;
try
{
flag = await this.DomainManager.DeleteAsync(id);
}
catch (HttpResponseException exception)
{
Services.Log.Error(exception, base.Request, LogCategories.TableControllers);
throw;
}
catch (Exception ex)
{
Services.Log.Error(ex, base.Request, LogCategories.TableControllers);
}
if (!flag)
{
Services.Log.Warn("Resource not found", base.Request);
}
}
}
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                