Silverlight WCF RIA服务(六)创建RIA Services 类库

Silverlight WCF RIA服务(六)创建RIA Services 类库,第1张

概述RIA Services 类库允许我们创建能够重复使用的中间层和表现层逻辑。然而,使用RIA Services类库要比创建RIA Services解决方案复杂的多。 在本节演练中,将创建一个拥有RIA Services类库代码的SL应用程序。简单起见,把类库放在了SL应用程序相同的解决方案里。当然,类库也可以放在分开的解决方案中。 创建包含WCF RIA Services类库的SL解决方案   在 RIA Services 类库允许我们创建能够重复使用的中间层和表现层逻辑。然而,使用RIA Services类库要比创建RIA Services解决方案复杂的多。
在本节演练中,将创建一个拥有RIA Services类库代码的SL应用程序。简单起见,把类库放在了SL应用程序相同的解决方案里。当然,类库也可以放在分开的解决方案中。

创建包含WCF RIA Services类库的SL解决方案

 
在VS中,创建一个命名为ExampleSilverlightApp的新SL应用程序。

在 新Silverlight应用程序 对话框中,不要勾选 WCF RIA Services 选项。这个应用程序不需要SL客户端和服务端之间的RIA Services link,因为这个RIA Services link将放在类库中。

在资源管理器中,右键点击解决方案,选择 添加->新建项目。 出现添加新项目对话框。

在Silverlight类型中,选择WCF RIA Services Class library模板并命名为AdvertureWorksClasslibrary。



点击OK。在解决方案中将包含四个项目,如下所示:

 


 
右键点击 ExampleSilverlightApp.Web 项目,并选择 添加引用。 添加引用对话框出现。

在 项目 标签中,选择 AdventureWorksClasslibrary.Web 项目,点击 OK。
右键点击 ExampleSilverlightApp 项目,选择 添加引用。
在 项目 标签中, 选择 AdventureWorksClasslibrary 项目,点击 OK。
 

创建中间层库


 
在 AdventureWorksClasslibrary.Web项目中,添加一个名为 AdventureWorksModel.edmx的 ADO.NET Entity Data Model。
在实体数据模型向导中,把 Product 表加到实体模型中。
生成解决方案。
右键点击 AdventureWorksClasslibrary.Web项目,选择 添加->新项。
选择 Domain Service Class 模板,并命名为ProductsDomainService。
点击 添加。 出现 添加新域服务类 对话框。
从domain service中提供的数据模型中选择 Product, 并点击 OK。
生成解决方案。
在解决方案中,对每个项目选择 显示所有文件-Show All files。我们可以发现仅在AdventureWorksClasslibrary项目中存在Generated_Code文件夹。虽然没有为ExampleSilverlightApp项目生成代码,但我们仍可以使用在AdventureWorksClasslibrary项目中生成的代码。因为在ExampleSilverlightApp项目和AdventureWorksClasslibrary项目间存在项目引用。
 

在SL项目中使用生成的代码


 
右键点击ExampleSilverlightApp项目,选择 添加引用。
添加对 System.windows.Ria 程序集的引用。通过导航到[Program files]\Microsoft SDKs\RIA Services\v1.0\livryrIEs\Silverlight,可以找到这个程序集。
在ExampleSilverlightApp项目中,打开MainPage.xaml文件。
从工具箱中,拖拽DataGrID控件到GrID内。 这会自动添加一个XML命名空间和一个数据程序集引用。
命名DataGrID为 ProductsGrID,如下所示:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?XML:nameSPACE PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><usercontrol class=RIAServicesExample.MainPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" data="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data" x="http://schemas.microsoft.com/winfx/2006/xaml" d="http://schemas.microsoft.com/expression/blend/2008" mc="http://schemas.openxmlformats.org/markup-compatibility/2006" ignorable="d" designwIDth="400" designheight="300">           <grID name="LayoutRoot" background="White">         <?xml:namespace prefix = data ns = "http://www.google.com/2005/gml/data" /><data:datagrID name="ProductsGrID"></data:datagrID>       </grID>   </usercontrol>

 
打开MainPage.xaml的后台代码。
添加下面的代码来检索产品。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 using System;   using System.Collections.Generic;   using System.linq;   using System.Net;   using System.windows;   using System.windows.Controls;   using System.windows.documents;   using System.windows.input;   using System.windows.Media;   using System.windows.Media.Animation;   using System.windows.Shapes;   using AdventureWorksClasslibrary.Web;   using System.windows.Ria;       namespace ExampleSilverlightApp   {       public partial class MainPage : UserControl       {           private ProductDomainContext _productContext = new ProductDomainContext();               public MainPage()           {               InitializeComponent();                   LoadOperation<?XML:nameSPACE PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><product> loadOp = this._productContext.Load(this._productContext.GetProductsquery());               ProductsGrID.ItemsSource = loadOp.EntitIEs;           }       }   }

 
打开AdventureWorksClasslibrary.Web项目中的App.Config文件,分别拷贝<connectionStrings>,<system.serviceModel>,和<httpModules>元素。把它们粘贴到ExampleSilverlightApp.Web项目的Web.config文件中。现在Web.config文件看上去和下面的代码类似,当然你应该提供和你的环境相关的链接信息。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 <?XML:nameSPACE PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><configuration>       <connectionstrings>           <add name="AdventureWorksLT2008EntitIEs" provIDer="System.Data.sqlClIEnt;provIDer" provIDername="System.Data.EntityClIEnt" connectionstring="'Metadata=" string="Data Source=example;Initial Catalog=AdventureWorksLT2008;Integrated Security=True;MultipleActiveResultSets=True">       </connectionstrings>       <system.servicemodel>           <servicehostingenvironment aspnetcompatibilityenabled="true">       </system.servicemodel>       <system.web>           <compilation targetframework="4.0" deBUG="true">       <httpmodules>           <add name="DomainServiceModule" type="System.Web.Ria.Services.DomainServicehttpModule,System.Web.Ria,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35">       </httpmodules>       </system.web>       <system.webserver>         <modules runallmanagedmodulesforallrequests="true">       </system.webserver>       </configuration>

 
运行应用程序。看到如下结果。

总结

以上是内存溢出为你收集整理的Silverlight WCF RIA服务(六)创建RIA Services 类库全部内容,希望文章能够帮你解决Silverlight WCF RIA服务(六)创建RIA Services 类库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1065774.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存