[转]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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值