[转]WCF Data Services OData

http://martinwilley.com/net/data/wcfds.html

WCF Data Services

For .net 4.5, this is replaced by Microsoft.AspNet.Odata which uses ODataController (based on WebApi ApiController).

About

Formerly "Astoria" and "ADO.Net Data Services", this is the .Net ODATA implementation - ODATA being RESTful HTTP/JSON/ATOM xml access to databases.

OData

HTTP verbs: GET is get, POST is insert, PUT is update, MERGE is partial update, DELETE is delete

NB: PUT,MERGE and DELETE may be blocked by proxies/firewalls, so add the X-HTTP-Method header can be added to a POST request

  • Use http://localhost/wcfds.svc/$metadata to get metadata (like ?wsdl)
  • http://localhost/wcfds.svc/customers('ALFKI')
    get by primary key
  • http://localhost/wcfds.svc/customers/$count
  • http://localhost/wcfds.svc/customers?$orderby=Price desc
  • http://localhost/wcfds.svc/customers?$skip=20&$top=5
  • http://localhost/wcfds.svc/customers?$filter= Name eq 'Bill' and Price gt 10
  • http://localhost/wcfds.svc/customers?$filter= startswith(name, 'B') and year(Date) gt 2011 and round(Price) = 5
  • http://localhost/wcfds.svc/customers?$expand=Orders/Order_Details
    Eager loading

Server code

Reference System.Data.Services.Client.dll. The VS2010 item template for the endpoint is "WCF Data Service". The svc code behind is a DataService<T> with a static InitializeService() method which should expose a IQueryable<T> getter.

DataService<T> can be an Entity Framework ObjectContext, which exposes the ObjectSets (tables).

For custom classes that are exposed, add a [DataServiceKeyAttribute(pk)] to the class to denote the primary key (for http://localhost/wcfds.svc/custom('ALFKI') gets). For POCOs, add [DataContract(IsReference=true)] so properties are serialized as objects, not values (i.e. product.Category refers to a Category object).

Security

You can secure with the standard IIS/web.config settings including Windows, asp.net forms etc.

In the client library, you can set context.Credentials = new NetworkCredential(username, password) or use the context.SendingRequest event to set request headers such as Authorization.

In server InitializeService, configure SetEntitySetAccessRules

Service Operations

In InitializeService, config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); as applicable/

Expose sprocs as service operations: [WebGet]/[WebInvoke] methods- input parameters must be primitives, return must be primitive or IEnumerables/IQueryable.

Client

Adding a service reference to an ODATA service creates a proxy derived from DataServiceContext which looks like a normal EF ObjectContext. (MSDN concepts)

  • context.SaveChanges(SaveChangesOption.Batch) will batch changes (also: ContinueOnError, ReplaceOnUpdate)
    Catch DataServiceQueryException, DataServiceRequestException
  • It uses ATOM - to use JSON, use a WebClient with client.Headers["Accept"] = "application/json"
  • In a partial class, add [QueryInterceptor("Customers")]public Expression... OnQueryCustomers()
  • [ChangeInterceptor("Customers")] can also be used for validation (or subscribe to regular EntityChanging event)

For data-binding, wrap the objectsets in DataServiceCollection which extends ObservableCollection.

Queries

    • The entity collections (ObjectSets in EF) are DataServiceQuery<T> but otherwise it's mostly normal IQueryable
    • Eager load (ODATA $expand) with context.Orders.Expand("Order_Details")
    • Explicit load (lazy load) with context.LoadProperty(order, "Order_Details")
    • Explicitly executing the query returns a QueryOperationResponse.
      When paging, this has a GetContinuation property which includes the url of the next page
      NB: you must iterate the query result to use the continuation
上一篇:[bzoj5472] 数列


下一篇:$.trim() 去除空格方法 (验证使用)