[转]ASP.NET web API 2 OData enhancements
Along with the release of Visual Studio 2013 came a new release of ASP.NET MVC (called MVC 5) and ASP.NET Web API (called Web API 2). Part of the enhancements to the Web API stack include some much needed features for OData that were missing in the initial release of the ASP.NET Web API OData that came out in June 2013. There were four new OData capabilities added in this release: $select, $expand, $value, and batch updates.
A quick review of the basicsOData is an open standard specification for exposing and consuming CRUD-centric (Create-Read-Update-Delete) data services over HTTP. OData follows most of the design guidelines of the REST architectural style and makes it very easy to implement data services or consume them on just about any modern development platform. OData has two main parts to it: the OData URL query syntax that allows you to perform rich queries from the client including filtering, sorting, and paging; and the full OData communication protocol that includes OData message formatting standards and the supported operations and calling patterns for performing CRUD operations via OData.
The first release of Web API OData added support for OData services using ASP.NET Web API. This included support for the OData query syntax for any Web API service that exposes IQueryable collection results via GET operations, as well as implementing an OData protocol compliant service that supports full CRUD operations on an object model with relationships between the entities. Let‘s focus on services that are “full” OData services with Web API.
The first step in using OData within your ASP.NET Web API site is to pull in the ASP.NET Web API 2 OData NuGet package.
When defining a Web API OData service that supports CRUD operations, you will generally want to inherit from the EntitySetController<EntityType,KeyType> base class. The base class exposes virtual methods for you to override for each of the CRUD operations mapping to the POST (Create), GET (Retrieve), PUT/PATCH (Update), and DELETE (Delete) HTTP verbs. For Create and Update operations, the base class handles the request and dispatches the call to a virtual method that lets you just deal with the entity being updated or created instead of handling the raw request yourself.
A simple controller dealing with Customers in a database for a Pizza ordering domain (Zza) is shown below.
public class CustomersController : EntitySetController<Customer, Guid> { ZzaDbContext _Context = new ZzaDbContext(); [Queryable()] public override IQueryable Get() { return _Context.Customers; } protected override Customer GetEntityByKey(Guid key) { return _Context.Customers.FirstOrDefault(c => c.Id == key); } protected override Guid GetKey(Customer entity) { return entity.Id; } protected override Customer CreateEntity(Customer entity) { if (_Context.Customers.Any(c=>c.Id == entity.Id)) throw ErrorsHelper.GetForbiddenError(Request, string.Format("Customer with Id {0} already exists", entity.Id)); _Context.Customers.Add(entity); _Context.SaveChanges(); return entity; } protected override Customer UpdateEntity(Guid key, Customer update) { if (!_Context.Customers.Any(c => c.Id == key)) throw ErrorsHelper.GetResourceNotFoundError(Request); update.Id = key; _Context.Customers.Attach(update); _Context.Entry(update).State = System.Data.Entity.EntityState.Modified; _Context.SaveChanges(); return update; } protected override Customer PatchEntity(Guid key, Delta patch) { var customer = _Context.Customers.FirstOrDefault(c => c.Id == key); if (customer == null) throw ErrorsHelper.GetResourceNotFoundError(Request); patch.Patch(customer); _Context.SaveChanges(); return customer; } public override void Delete(Guid key) { var customer = _Context.Customers.FirstOrDefault(c => c.Id == key); if (customer == null) throw ErrorsHelper.GetResourceNotFoundError(Request); _Context.Customers.Remove(customer); _Context.SaveChanges(); } protected override void Dispose(bool disposing) { _Context.Dispose(); base.Dispose(disposing); } }温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/40457.html