zoukankan      html  css  js  c++  java
  • Silverlight WCF RIA服务(六)创建RIA Services 类库

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

      1. 在实体数据模型向导中,把 Product 表加到实体模型中。

      1. 生成解决方案。

      1. 右键点击 AdventureWorksClassLibrary.Web项目,选择 添加->新项

      1. 选择 Domain Service Class 模板,并命名为ProductsDomainService。

      1. 点击 添加。 出现 添加新域服务类 对话框。

      1. 从domain service中提供的数据模型中选择 Product, 并点击 OK

      1. 生成解决方案。

    1. 在解决方案中,对每个项目选择 显示所有文件-Show All Files。我们可以发现仅在AdventureWorksClassLibrary项目中存在Generated_Code文件夹。虽然没有为ExampleSilverlightApp项目生成代码,但我们仍可以使用在AdventureWorksClassLibrary项目中生成的代码。因为在ExampleSilverlightApp项目和AdventureWorksClassLibrary项目间存在项目引用。

     

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


     

      1. 右键点击ExampleSilverlightApp项目,选择 添加引用

      1. 添加对 System.Windows.Ria 程序集的引用。通过导航到[Program Files]\Microsoft SDKs\RIA Services\v1.0\Livryries\Silverlight, 可以找到这个程序集。

      1. 在ExampleSilverlightApp项目中,打开MainPage.xaml文件。

      1. 从工具箱中,拖拽DataGrid控件到Grid内。 这会自动添加一个XML命名空间和一个数据程序集引用。

      1. 命名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/presentationNS "http://schemas.microsoft.com/winfx/2006/xaml/presentation" /><usercontrolclass=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="ProductsGrid"></data:datagrid>
         
            </grid>
         
        </usercontrol>

         

      1. 打开MainPage.xaml的后台代码。

      1. 添加下面的代码来检索产品。
        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;
         
                }
         
            }
         
        }

         

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

         

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

    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    【转载】使用铁哥SmartFlash快速开发方案:66行代码搞定抽奖程序!
    WPF 数据绑定方法分类
    jquerywebsocket
    [转载]as3中单例模式如何设计
    .字符的匹配识别
    paip.提升用户体验搜索功能设计
    paip.提升安全性登录密码出错次数检测
    paip.html 及css调试工具debugbar
    paip.项目开发效率提升之思索
    paip.c#图片裁剪
  • 原文地址:https://www.cnblogs.com/Areas/p/2172155.html
Copyright © 2011-2022 走看看