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

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。

 

创建中间层库


 

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

 

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


 

  1. 右键点击ExampleSilverlightApp项目,选择 添加引用
  2. 添加对 System.Windows.Ria 程序集的引用。通过导航到[Program Files]\Microsoft SDKs\RIA Services\v1.0\Livryries\Silverlight, 可以找到这个程序集。
  3. 在ExampleSilverlightApp项目中,打开MainPage.xaml文件。
  4. 从工具箱中,拖拽DataGrid控件到Grid内。 这会自动添加一个XML命名空间和一个数据程序集引用。
  5. 命名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>

     
  6. 打开MainPage.xaml的后台代码。
  7. 添加下面的代码来检索产品。
    ?
    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;
     
            }
     
        }
     
    }

     
  8. 打开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
     
        <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>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值