public static class WebApiConfig { public static void Register() { ServiceConfig.Initialize(new ConfigBuilder()); } }
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { WebApiConfig.Register(); } }
public class Order : DocumentData { public DateTime OrderDate { get; set; } public string Client { get; set; } public List<OrderItem> Items { get; set; } } public class OrderItem { public string Name { get; set; } public double Quantity { get; set; } public double Price { get; set; } }
ItableData
interface as well as it does for the Entity Framework data models. To implement this interface in the Entity Framework, we use the base class EntityData
,
which is similar to the DocumentData
class when using MongoDB .
After that, we can only determine the properties of the subject area in the class model. public class OrderController : TableController<Order> { protected override void Initialize(HttpControllerContext controllerContext) { base.Initialize(controllerContext); var connStringName = "mongodb"; var dbName = "MyMongoLab"; var collectionName = "orders"; this.DomainManager = new MongoDomainManager<Order>(connStringName, dbName, collectionName, this.Request, this.Services); } public IQueryable<Order> GetAllOrders() { return base.Query(); } public Order GetOneOrder(string id) { var result = base.Lookup(id).Queryable.FirstOrDefault(); if (result == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } else { return result; } } public Task<Order> PostOrder(Order order) { return base.InsertAsync(order); } public Task DeleteOrder(string id) { return base.DeleteAsync(id); } public Task<Order> PatchOrder(string id, Delta<Order> patch) { return base.UpdateAsync(id, patch); } }
MongoDomainManager
constructor is the name of the element from the < connectionStrings> section in the configuration that contains the actual connection string to the database (we can later add a function to pass the actual connection string to the constructor). <connectionStrings> <add name="mongodb" connectionString="mongodb://MyMongoLab:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-@dsNNNNNN.mongolab.com:PPPPP/MyMongoLab"/> </connectionStrings>
GET http://localhost:54524/tables/order HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 2 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 15:43:31 GMT []
POST http://localhost:54524/tables/order HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 Content-Length: 211 Content-Type: application/json { "client":"John Doe", "orderDate":"2014-04-13T00:00:00Z", "items":[ { "name": "bread", "quantity": 1, "price": 1.99 }, { "name": "milk", "quantity": 2, "price": 2.99 } ] } =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Content-Length: 383 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 15:53:13 GMT { "orderDate": "2014-04-13T00:00:00Z", "client": "John Doe", "items": [ { "name": "bread", "quantity": 1.0, "price": 1.99 }, { "name": "milk", "quantity": 2.0, "price": 2.99 } ], "id": "534c0469f76e1e10c4703c2b", "__createdAt": "2014-04-14T15:53:12.982Z", "__updatedAt": "2014-04-14T15:53:12.982Z" }
POST http://localhost:54524/tables/order HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 Content-Length: 216 Content-Type: application/json { "client":"Jane Roe", "orderDate":"2014-02-22T00:00:00Z", "items":[ { "name": "nails", "quantity": 100, "price": 3.50 }, { "name": "hammer", "quantity": 1, "price": 12.34 } ] } =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Content-Length: 387 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 15:53:21 GMT { "orderDate": "2014-02-22T00:00:00Z", "client": "Jane Roe", "items": [ { "name": "nails", "quantity": 100.0, "price": 3.5 }, { "name": "hammer", "quantity": 1.0, "price": 12.34 } ], "id": "534c0471f76e1e10c4703c2c", "__createdAt": "2014-04-14T15:53:21.557Z", "__updatedAt": "2014-04-14T15:53:21.557Z }
GET http://localhost:54524/tables/order HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 239 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 15:55:12 GMT [ { "id": "534c0469f76e1e10c4703c2b", "client": "John Doe", "orderDate": "2014-04-13T00:00:00Z" }, { "id": "534c0471f76e1e10c4703c2c", "client": "Jane Roe", "orderDate": "2014-02-22T00:00:00Z" } ]
public List<Order> GetAllOrders() { return base.Query().ToList(); }
GET http://localhost:54524/tables/order?$expand=items HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 663 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 17:52:26 GMT [ { "id": "534c0469f76e1e10c4703c2b", "client": "John Doe", "orderDate": "2014-04-13T00:00:00Z", "items": [ { "name": "bread", "quantity": 1.0, "price": 1.99 }, { "name": "milk", "quantity": 2.0, "price": 2.99 } ] }, { "id": "534c0471f76e1e10c4703c2c", "client": "Jane Roe", "orderDate": "2014-02-22T00:00:00Z", "items": [ { "name": "nails", "quantity": 100.0, "price": 3.5 }, { "name": "hammer", "quantity": 1.0, "price": 12.34 } ] } ]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] class ExpandPropertyAttribute : ActionFilterAttribute { string propertyName; public ExpandPropertyAttribute(string propertyName) { this.propertyName = propertyName; } public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); var uriBuilder = new UriBuilder(actionContext.Request.RequestUri); var queryParams = uriBuilder.Query.TrimStart('?').Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToList(); int expandIndex = -1; for (var i = 0; i < queryParams.Count; i++) { if (queryParams[i].StartsWith("$expand", StringComparison.Ordinal)) { expandIndex = i; break; } } if (expandIndex < 0) { queryParams.Add("$expand=" + this.propertyName); } else { queryParams[expandIndex] = queryParams[expandIndex] + "," + propertyName; } uriBuilder.Query = string.Join("&", queryParams); actionContext.Request.RequestUri = uriBuilder.Uri; } }
[ExpandProperty("Items")] public IQueryable<Order> GetAllOrders() { return base.Query(); }
GET http://localhost:54524/tables/order?$orderby=client HTTP/1.1 User-Agent: Fiddler Host: localhost:54524 =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 663 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 18:37:27 GMT [ { "id": "534c0471f76e1e10c4703c2c", "client": "Jane Roe", "orderDate": "2014-02-22T00:00:00Z", "items": [ { "name": "nails", "quantity": 100.0, "price": 3.5 }, { "name": "hammer", "quantity": 1.0, "price": 12.34 } ] }, { "id": "534c0469f76e1e10c4703c2b", "client": "John Doe", "orderDate": "2014-04-13T00:00:00Z", "items": [ { "name": "bread", "quantity": 1.0, "price": 1.99 }, { "name": "milk", "quantity": 2.0, "price": 2.99 } ] } ]
GET http://blog20140413.azure-mobile.net/tables/order HTTP/1.1 User-Agent: Fiddler Host: blog20140413.azure-mobile.net =-=-=-=-=-=-=-=-=- HTTP/1.1 500 Internal Server Error Cache-Control: no-cache Pragma: no-cache Content-Length: 43 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 18:50:22 GMT { "message": "An error has occurred." }
Exception=System.ArgumentException: No connection string named 'mongodb' could be found in the service configuration. at Microsoft.WindowsAzure.Mobile.Service.MongoDomainManager`1.GetMongoContext(String connectionStringName) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Microsoft.WindowsAzure.Mobile.Service.MongoDomainManager`1..ctor(String connectionStringName, String databaseName, String collectionName, HttpRequestMessage request, ApiServices services) at MongoDbOnNetBackend.OrderController.Initialize(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext(), Id=6133b3eb-9851-4
static bool connectionStringInitialized = false; private void InitializeConnectionString(string connStringName, string appSettingName) { if (!connectionStringInitialized) { connectionStringInitialized = true; if (!this.Services.Settings.Connections.ContainsKey(connStringName)) { var connFromAppSetting = this.Services.Settings[appSettingName]; var connSetting = new ConnectionSettings(connStringName, connFromAppSetting); this.Services.Settings.Connections.Add(connStringName, connSetting); } } } protected override void Initialize(HttpControllerContext controllerContext) { var connStringName = "mongodb"; var dbName = "MyMongoLab"; var collectionName = "orders"; // Workaround for lack of connection strings in the portal InitializeConnectionString(connStringName, "mongoConnectionString"); base.Initialize(controllerContext); this.DomainManager = new MongoDomainManager<Order>(connStringName, dbName, collectionName, this.Request, this.Services); }
GET http://blog20140413.azure-mobile.net/tables/order HTTP/1.1 User-Agent: Fiddler Host: blog20140413.azure-mobile.net x-zumo-application: cOFQkbaAmffuVRBJRpYDKHbNHbtDYG97 =-=-=-=-=-=-=-=-=- HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 663 Content-Type: application/json; charset=utf-8 Expires: 0 Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Date: Mon, 14 Apr 2014 19:21:11 GMT [ { "id": "534c0469f76e1e10c4703c2b", "client": "John Doe", "orderDate": "2014-04-13T00:00:00Z", "items": [ { "name": "bread", "quantity": 1.0, "price": 1.99 }, { "name": "milk", "quantity": 2.0, "price": 2.99 } ] }, { "id": "534c0471f76e1e10c4703c2c", "client": "Jane Roe", "orderDate": "2014-02-22T00:00:00Z", "items": [ { "name": "nails", "quantity": 100.0, "price": 3.5 }, { "name": "hammer", "quantity": 1.0, "price": 12.34 } ] } ]
Source: https://habr.com/ru/post/239633/
All Articles