[转载]oData另一种不用写代码就在网上提供数据服务的方法

[转载]oData – 另一种不用写代码就在网上提供数据服务的方法 – Footprints on the sands of time – 博客园.

先看两个例子:

这里还有更多提供/支持这种服务的链接:http://www.odata.org/blog/2010/10/28/new-odata-producers-and-consumers

这是什么?

开放数据协议(oData)让你重新认识 HTTP 协议并且简化数据在网上的发布和更新过程。参看在 cnblogs 查到的唯一介绍它的新闻:http://news.cnblogs.com/n/69756/ 和一篇写在 VS 2010 RC 时候的简介:http://www.cnblogs.com/shanyou/archive/2010/02/11/1667381.html

说正题: 用 VS 2010 + .NET V4,几乎可以不用写代码就把完整的数据服务发布到网上步骤如下:

假设你已经有一个数据库,用户可以发围脖和订阅别人的围脖,数据结构如下,

要把它变成一个网络数据服务:

1、在VS 2010 建一个Web应用或网站,加一个数据模型:

2、把数据库里的表加进来:

把默认生成的关联属性名字改改,改好就像右边这样:

3、加一个新的 WCF Data Service 网页:

4、打开它的 CS 文件,

代码

1 public class messages : DataService< /* TODO: put your data source class name here */ >
2 {
3 // This method is called only once to initialize service-wide policies.
4 public static void InitializeService(DataServiceConfiguration config)
5 {
6 // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
7 // Examples:
8 // config.SetEntitySetAccessRule(“MyEntityset”, EntitySetRights.AllRead);
9 // config.SetServiceOperationAccessRule(“MyServiceOperation”, ServiceOperationRights.All);
10 config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
11 }
12 }

改成下面这个样子(基本上就是把注释去掉,把刚才的数据模型加进来):

代码

1 public class messages : DataService<MessageEntities>
2 {
3 public static void InitializeService(DataServiceConfiguration config)
4 {
5 config.SetEntitySetAccessRule(*, EntitySetRights.AllRead);
6 config.SetServiceOperationAccessRule(*, ServiceOperationRights.All);
7 config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
8 }
9 }

5、没了,你的数据服务已经在网上。

浏览你的SVC网页,可以看到:

代码

1 <?xml version=”1.0″ encoding=”utf-8″ standalone=”yes” ?>
2 <service xml:base=”http://localhost:38791/messages.svc/” xmlns:atom=”http://www.w3.org/2005/Atom” xmlns:app=”http://www.w3.org/2007/app” xmlns=”http://www.w3.org/2007/app”>
3 <workspace>
4 <atom:title>Default</atom:title>
5 <collection href=”Posts”>
6 <atom:title>Posts</atom:title>
7 </collection>
8 <collection href=”Subscriptions”>
9 <atom:title>Subscriptions</atom:title>
10 </collection>
11 <collection href=”Users”>
12 <atom:title>Users</atom:title>
13 </collection>
14 </workspace>
15 </service>
16

浏览service.svc/Users,可以看到用户列表:

代码

<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes” ?>

<feed xml:base=”http://localhost:38791/messages.svc/” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns=”http://www.w3.org/2005/Atom”>
<title type=”text”>Users</title>
<id>http://localhost:38791/messages.svc/Users</id>
<updated>2010-12-20T19:22:12Z</updated>
<link rel=”self” title=”Users” href=”Users” />
<entry>
<id>http://localhost:38791/messages.svc/Users(1)</id>
<title type=”text” />
<updated>2010-12-20T19:22:12Z</updated>
<author>
<name />
</author>
<link rel=”edit” title=”User” href=”Users(1)” />
<link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Posts” type=”application/atom+xml;type=feed” title=”Posts” href=”Users(1)/Posts” />
<link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Followers” type=”application/atom+xml;type=feed” title=”Followers” href=”Users(1)/Followers” />
<link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Subscriptions” type=”application/atom+xml;type=feed” title=”Subscriptions” href=”Users(1)/Subscriptions” />
<category term=”Message.DataModel.User” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme” />
<content type=”application/xml”>
<m:properties>
<d:id m:type=”Edm.Int32″>1</d:id>
<d:name>Test</d:name>
</m:properties>
</content>
</entry>
</feed>

总结

为什么叫开放数据协议?因为它跨语言和平台的,基于http地址的数据服务。

例子:

列出所有的用户:service.svc/Users

找一个叫张三的用户:service.svc/Users?$filter=Name eq ‘张三’

他发的围脖(假设前一个查询返回了他的ID):service.svc/Users(1)/Posts

他的Fans: service.svc/Users(1)/Followers

他发的关于cnblogs的围脖:service.svc/Posts?$filter=userId eq 1 and indexof(content, ‘cnblogs’) ge 0

这些看起来并不难,但好处是,它们都不需要写任何代码,而且它是RESTful的,任何可以访问URL的语言,平台都可以用它。除此以外,这只是GET操作,POST,DELETE等其他标准HTTP操作也是原生支持的。所以,它是完整的数据服务,并不仅仅是数据查询。

如果大家有兴趣,我会继续写这个主题,比如在WPF使用这种服务,在JavaScript使用(Built in JSON support, 哈哈),并涉及一些更深的,实际项目的内容,比如数据安全,数据事务。

题外话, 为什么是另一个?很久很久以前,SQL Server曾经有过Web Service 的服务,但M$做了一个艰难的决定,把它在后续版本中移除了。由于 Entity Framework, WCF data service in .NET V4的加持,这种服务又换了一个方式回来了。

题外外话,斗转星移,离上一篇文章又两年过去了,地方没变,人老了。

人老记性就不好,这个给忘了:要修给IE的一个设置,让它不自动 turn on feed view,不然你看到就是IE格式化过的内容了,odata返回的是,人也可以吃,IE也可以吃的 Atom xml.

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏