Silverlight WCF RIA服务 (三)创建一个RIA Services Solution

在本节,我们将进行一个演练。我们创建一个WCF RIA Services 应用,此应用从AdventureWorksLT 数据库中检索数据。我们会用SL来创建表现层的客户端,并创建表现不同数据库表的实体类。
  

在本节,我们将进行一个演练。我们创建一个WCF RIA Services 应用,此应用从AdventureWorksLT 数据库中检索数据。我们会用SL来创建表现层的客户端,并创建表现不同数据库表的实体类。

此节需要:



 


  • Visual Studio 2008
     
  • Silverlight 3
     
  • WCF RIA Services Beta for visual Studio 2008 SP1

-or-

 

  • Visual Studio 2010 Beta 2 
     
  • Silverlight 4 Beta 
     
  • WCF RIA Services Preview for Visual Studio 2010 
     
  • AdventureWorksLT sample database




 

创建包含RIA Services Link的解决方案

 

  1. 在VS中,创建一个新的RIA Services项目,选择 File->New->Project。此时出现新项目对话框。 
     
  2. 选择Silverlight应用程序模板,并命名为RIAServicesExample. 
     
  3. 点击OK。出现New Silverlight Application对话框出现。 
     
  4. 选择Enable WCF RIA Services选项框。现在将会在客户端和服务端之间创建连接。 
     
  5. 点击OK来创建解决方案。这个方案包含两个项目:客户端和服务端。客户端被命名为RIAServicesExample,其中有用来创建表示层的SL代码。服务端被命名为RIAServicesExample.Web,其中包含中间层的代码。



创建数据模型 Data Models

在这部分,创建表示来自AdventureWorksLT数据库的数据的ADO.NET实体类。RIA Services与各种各样的数据模型类和数据源一起工作。

(注:在VS2010中使用实体数据模型时,应该选择Include foreign key columns in the model选项。当用实体数据模型向导时,这个选项是默认选择的。还应该使用在程序集内嵌入的实体数据模型映射信息的默认行为。)

---使数据在中间层可利用

  1. 在解决方案资源管理器中,右键点击服务端项目(RIAServicesExample.Web),选择添加->新项目, 添加新项目对话框将出现。 
     
  2. 在类别列表中,选择数据并选择ADO.NET Entity Data Model模板。 
     
  3. 命名为AdventureWorksModel.edmx并点击添加。 出现实体数据模型向导。 
     
  4. 在 选择模型内容 中,选择 从数据库生成 ,然后点击下一步。 
     
  5. 在 选择数据连接 中,创建到数据库的数据连接并点击下一步。 
     
  6. 在 选择数据库对象 里,选择 Address, Customer, CustomerAddress表。 
     
  7. 点击 完成 ,就为表生成了实体模型。 
     
  8. 生成解决方案

创建域服务Domain Service

在这节中,将添加一个对中间层的domain service。domain service 把服务端的数据实体和操作
向客户端公开。可以在数据服务中加入商业逻辑来管理客户端如何与数据交互。

---创建domain service


 

  1. 右键点击服务端项目,选择 添加->新项目。 
     
  2. 在列表中选择 Web, 然后选择 Domain Service Class 模型。 
     
  3. 命名类为CustomerDomainService.cs(或 CustomerDomainService.vb)。 
     
  4. 点击 添加。 出现 添加新域服务类 对话框。 
     
  5. 选择自定义实体。 
     
  6. 确定选了,Enable client access选项, 选择对应 自定义 的 Enable Editing选项。
  7.  
  8.  
     
  9. 点击 OK,就生成了domain service 类。 
     
  10. 打开CustomerDomainServie.cs文件。会注意到文件有如下属性:



 

  • CustomerDomainService类派生于LinqToEntitiesDomainService(TContex),这个基类是RIA Services框架内的一个抽象类。由于domain Services公开了ADO.NET 实体数据类,此基类是自动应用的。 
     
  • 一个泛型基类被绑定到上一步创建的实体类。 
     
  • CustomerDomainService类被标记上EnableClientAccess属性,来指出这个类是可以被客户层访问的。 
     
  • 生成了一个查询方法。在VS2008中,这个查询叫做GetCustomer。在VS2010Beta2中,这个查询方法叫做GetCustomers。这个方法返回没有过滤和排序的所有项。 
     
  • 还生成了 InsertCustomer, UpdateCustomer, DeleteCustomer方法。

创建Silverlight客户端

在其他的演练中,将在domain service里加入商业逻辑。在本演练中,只是用生成的GetCustomers 方法。

由于在客户端和服务端存在着一个RIA Services Link,当生成解决方案时就会产生客户代理类。这个代理类允许我们从客户端访问数据。

--- 查看产生的客户端代理类


 

  1. 生成解决方案 
     
  2. 在解决方案资源管理器中,在客户端项目中点击显示所有文件。注意到在Generated_Code文件夹中包含了一个代码文件。





 

3. 打开这个代码文件。注意到文件有下面的属性:

  • 生成一个派生于WebContextBase类的WebContext类。 
     
  • 生成一个派生于DomainContext类的CustomerDomianContext类。这个类中有一个叫做GetCustomersQuery(在VS2008中是GetCustomerQuery)方法,它对应于在domain service中创建的查询方法。 
     
  • 生成一个派生于Entity类的Customer类,此类对应着domain service公开的实体。这个客户端的Customer实体类对应于服务端的Customer实体。




 

在Silverlight客户端中显示数据




 

  1. 打开 MainPage.xaml。 
     
  2. 从工具箱里,拖拽一个DataGrid控件到Grid里。当从工具箱中拖拽DataGrid控件时,会自动添加一个XML命名空间和一个对数据程序集的引用。如果你不是从工具箱中拖拽的DataGrid控件,你需要添加一个对System.Windows.Controls.Data程序集的引用和一个XML命名空间 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
     
  3. 命名这个控件为 CustomerGrid,如下面所示:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?XML:NAMESPACE  PREFIX = [default] http://schemas.microsoft.com/winfx/2006/xaml/presentation NS  "http://schemas.microsoft.com/winfx/2006/xaml/presentation"  /><usercontrol class=RIAServicesExample.MainPagexmlns="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="CustomerGrid"></data:datagrid>
     
        </grid>
     
    </usercontrol>

     
  4. 打开MainPage.xaml的后台代码文件。 
     
  5. 添加RIAServiceExample.Web命名空间和System.Windows.Ria命名空间。RIAServices.Web命名空间位于客户端的生成代码里。 
     
  6. 添加CustmerDomainContext实例,通过GetCustomerQuery方法来检索信息,并绑定到DataGrid控件,如下代码所示:
    ?
    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
    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  RIAServicesExample.Web;
     
    using  System.Windows.Ria;
     
     
     
    namespace  RIAServicesExample
     
    {
     
        public  partial  class  MainPage : UserControl
     
        {
     
            private  CustomerDomainContext _customerContext = new  CustomerDomainContext();
     
     
     
            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" /><customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
     
                CustomerGrid.ItemsSource = loadOp.Entities;
     
            }
     
        }
     
    }

     
  7. 运行应用程序。将会得到一个如下类似的表格:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值