拼吾爱程序人生

首页 » .Net编程 » Silverlight » Using ADO.NET Data Services in Silverlight 2 Beta 2
cobra - 2008-6-8 14:23:00
URL: http://msdn.microsoft.com/en-us/data/bb931106.aspx


 附件: 您所在的用户组无法下载或查看附件
Now that Silverlight 2 Beta 2 has launched, we have the ability to consume ADO.NET Data Services (formerly Astoria) from within Silverlight projects. ADO.NET Data Services are a perfect match for client-side technologies like Silverlight and ASP.NET AJAX.

For the uninitiated, ADO.NET Data Services is a new part of the .NET 3.5 framework that supports exposing a data model (e.g. LINQ for SQL, Entity Framework, etc.) as a set of queryable REST endpoints. ADO.NET Data Services maps the four data verbs into the four HTTP verbs:


Essentially it provides a way to use a data model across the firewall. It works by exposing IQueryable endpoints through a URI-based syntax allowing developers control over how the data is retrieved through:


In addition ADO.NET Data Services utilizes JSON and Atom (though Plain Old XML may be supported eventually) as the serialization formats. These make it easy to consume in client-side interfaces like Silverlight and AJAX. For more information on ADO.NET Data Services, see there site:
http://msdn.microsoft.com/en-us/data/bb931106.aspx

Before you can consume a Data Service in Silverlight, you will need to create a model and service.  See Guy Burstein's walkthrough of creating the service here:
http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/05/12/visual-studio-2008-sp1-ado-net-data-service-walkthrough.aspx
You *can* use LINQ to SQL as your data model but currently it does not have support for updating via ADO.NET Data Services so if you need to read and write data, you should start with an Entity Framework data model.

In Silverlight 2 Beta 2, ADO.NET Data Services is composed of an in-memory library allows asynchronous LINQ queries that are translated into the URI syntax automatically. Before you can get started you will need a a client-side version of the Data Context object and data contract classes for the entities in your data model. To do this, simply use the DataSvcUtil.exe tool located in the C:\Windows\Microsoft.NET\Framework\v3.5\ directory. Typically you would call it by specifying the URI of the service, the name of the class file to create and what language to use:

Code


Once that class is created, add it to your Silverlight project.  You will also need to add a reference to the System.Data.Client.Services.dll assembly. Now we are ready to get some data. 
First we need to create an instance of the context class.  This class exposes each of the model types as queryable properties.  When creating an instance of the class, you must specify a URI to the service itself:

Code


Querying data with Data Services looks much like any other LINQ-based query:

Code


Normally, you could execute the query directly by calling something like ToList() against the query.  Since that operation would cause a synchronous web request to happen across the network, that isn't supported in Silverlight 2 Beta 2.  In fact, if you try you will simply get a NotImplemented exception. In order to execute these queries, you will need to cast the query into a DataServiceQuery.  The DataServiceQuery allows you to call BeginExecute to start an asynchronous query as seen below:

Code


Once the query completes, it will call the OnLoadComplete method that was specified in the AsyncCallback.  In this method, you first grab the DataServiceQuery that you sent with the request then end the execution to retrieve the results as seen below:

Code


While its not as straightforward as synchronous execution, the new ADO.NET Data Services certainly works well in Silverlight 2 Beta 2 and provides a great way to use existing or planned data models over Internet applications. There are a couple of caveats:


I am currently working on two examples for this (a simple one and a more complex one) as well as an article for MSDN, all of which will greatly expand the details of how to use ADO.NET Data Services (including how to save changes back to the server. Be sure to watch here for details of those samples and articles.

 您可能对 [Silverlight] 的这些文章也感兴趣:

Silverlight 2 (beta1)数据操作(4)
Silverlight 2 Beta 1版本缺陷列表
Silverlight 2 (beta1)数据操作(5)
Silverlight 2 (beta1)数据操作(6)
初尝Deep Zoom Composer
Silverlight 2 (beta1)数据操作(7)
推荐两个不错的基于SilverLight 2 Beta 应用
专访Silverlight中国团队:从事最底层CLR核心代码研发
使用Popfly GameCreator轻松构建Silverlight游戏--Part1
使用Popfly GameCreator轻松构建Silverlight游戏--Part2
Silverlight 2学习教程(一):开发Silverlight 2应用程序的准备工作
Silverlight 2学习教程(二):开发简单的Silverlight 2应用程序
1
查看完整版本: Using ADO.NET Data Services in Silverlight 2 Beta 2
Modify by pin5i DZNT_ExpandPackage 2.1.3295 2007-2009 pin5i.com
 Total Unique Visitors: