Share via


逐步解說:建立 RIA Services 方案

在本逐步解說中,您建立 WCF RIA Services 應用程式,此應用程式會從 AdventureWorksLT 資料庫中擷取資料,然後將其提供給顯示該資料庫的 Silverlight 用戶端。您從資料來源建立中介層的伺服器上表示各種資料庫資料表的實體類別,藉以存取資料,然後透過建立可讓這些中介層實體存取 Silverlight 用戶端的網域服務來呈現它。本逐步解說可做為 RIA Services 文件中許多其他逐步解說的起點。

必要條件

除了 WCF RIA Services 和 WCF RIA Services 工具組之外,在 RIA Services 文件中呈現的這個逐步解說和其他逐步解說還需要正確安裝並設定數個必要程式 (例如 Visual Studio 2010 和 Silverlight 開發人員執行階段與 SDK)。要執行逐步解說還需要安裝並設定 SQL Server 2008 R2 Express with Advanced Services,以及安裝 AdventureWorks OLTP 和 LT 資料庫。

WCF RIA Services 的必要條件節點中的主題也提供符合這些必要條件的詳細指示。請先按照該處提供的指示進行,然後再進行本逐步解說,以確保您在進行本 RIA Services 逐步解說時不會發生問題。

使用專案間的 RIA Services 連結建立方案

若要設定 RIA Services 方案

  1. 在 Visual Studio 2010 中建立新的 RIA Services 專案,方式是依序選取 [檔案]、[新增] 和 [專案]。

    [新增專案] 對話方塊隨即出現。

  2. 從 [已安裝的範本] 的 [Silverlight] 群組選取 [Silverlight 應用程式] 範本,並將新專案命名為 RIAServicesExample

    RIA_StartProject

  3. 按一下 [確定]。

    [新 Silverlight 應用程式] 對話方塊隨即出現。

  4. 選取靠近這個對話方塊底部的 [啟用 WCF RIA Services] 核取方塊。選取這個方塊會在用戶端專案和伺服器專案間建立 RIA Services 連結。

    RIA_CreateProjectwithLink

  5. 按一下 [確定] 來建立方案。

    這個方案包含兩個專案:一個用戶端專案和一個伺服器專案。用戶端專案命名為 RIAServicesExample,它包含您用來建立簡報層的 Silverlight 程式碼。伺服器專案命名為 RIAServicesExample.Web,它包含中介層程式碼。

    RIA_ProjectsCreated

建立資料模型

在本節中,您將建立表示 AdventureWorksLT 資料庫中資料的 ADO.NET 實體類別。RIA Services 可搭配各種資料模型類別和資料來源使用。如需使用 RIA Services 存取資料之選項的詳細資訊,請參閱資料主題。

Caution注意:
搭配 Visual Studio 2010 使用實體資料模型 (EMD) 時,您必須選取 [在模型中包含外部索引鍵資料行] 選項。根據預設,當您使用 [實體資料模型] 精靈時,會選取此選項。您也必須在組件中使用嵌入 EMD 對應資訊的預設行為。

若要讓資料可以在中介層中使用

  1. 以滑鼠右鍵按一下 [方案總管] 中的伺服器專案 RIAServicesExample.Web、選取 [加入],然後選取 [新增項目]。

    [加入新項目] 對話方塊隨即出現。

  2. 選取類別清單中的 [資料],然後選取 [ADO.NET 實體資料模型] 範本。

  3. 將新檔案命名為 AdventureWorksModel.edmx,然後按一下 [加入]。

    [實體資料模型精靈] 隨即出現。

  4. 選取 [選擇模型內容] 畫面中的 [從資料庫產生] 選項,然後按 [下一步]。

  5. 在 [選擇您的資料連接] 對話方塊中,建立資料庫的資料連接,然後按一下 [下一步]。

  6. [選擇您的資料庫物件] 畫面中,選取 [Address]、[Customer] 和 [CustomerAddress] 資料表。

  7. 確認 [在模型中包含外部索引鍵資料行] 核取方塊已依預設核取,然後按一下 [完成]。

    資料表的實體模型隨即建立。

  8. 建置 (使用 Ctrl+Shift+B 組合鍵) 方案。

建立網域服務

在本節中,您將在中介層專案中加入網域服務。網域服務會向用戶端專案公開伺服器專案中的資料實體和作業。您可以將商務邏輯加入至網域服務來管理用戶端與資料互動的方式。

若要建立網域服務

  1. 以滑鼠右鍵按一下伺服器專案,並選取 [加入] 和 [新增項目]。

  2. 選取類別清單中的 [網頁],然後選取 [DomainService 類別] 範本。

  3. 將類別命名為 CustomerDomainService.cs (或 CustomerDomainService.vb)。

    RIA_AddDomainService

  4. 按一下 [加入]。

    [加入新的 DomainService 類別] 對話方塊隨即出現。

  5. 請確定已核取 [啟用用戶端存取] 方塊。

  6. 選取 [Customer] 實體,然後為其核取 [啟用編輯] 方塊。

    RIA_AddEntity

  7. 按一下 [確定]。

    CustomerDomainService 類別是在新的 CustomerDomainService.cs (或 CustomerDomainService.vb) 檔案中產生。

  8. 開啟此檔案。請注意,此檔案具有下列特性:

    • CustomerDomainService 類別衍生自 LinqToEntitiesDomainService 類別,後者是 RIA Services 架構中的抽象基底類別。網域服務會公開一個 ADO.NET 實體資料類別,因此會自動使用此基底類別。

    • 泛型基底類別會繫結至先前步驟中,透過其氾型參數中型別 ObjectContextAdventureWorksLTEntities 所建立的實體類別。

    • CustomerDomainService 類別會以 EnableClientAccessAttribute 屬性標示,表示用戶端層看得到此類別。

    • 系統會產生名稱為 GetCustomers 的查詢方法。此方法會在沒有任何篩選或排序的情況下,傳回每個項目。

    • 從記錄中插入、更新和刪除客戶的方法已經建立。

建立 Silverlight 用戶端

在其他逐步解說中,您將會看到如何將商務邏輯加入至網域服務。在本逐步解說中,您只要使用預設產生的 GetCustomers 方法。

當您建置方案時,會產生用戶端 Proxy 類別。在用戶端專案和伺服器專案之間建立的 RIA Services 連結能夠產生此程式碼。這些用戶端 Proxy 類別可存取來自用戶端的資料。

若要查看產生的用戶端 Proxy 類別

  1. 建置方案。

    當您建置專案時,會在用戶端專案中產生程式碼。

  2. 選取 [方案總管] 中的 [RIAServicesExample] 用戶端專案,然後按一下視窗頂部的 [顯示所有檔案] 圖示。

    請注意,Generated_Code 資料夾包含 RIAServicesExample.Web.g.cs (或 RIAServicesExample.Web.g.vb) 檔案。

    RIA_GeneratedCode

  3. 開啟 Generated_Code 資料夾中的程式碼檔案。

    請注意,此檔案具有下列特性:

    • 系統會產生衍生自 WebContextBase 類別的 WebContext 類別。

    • 系統會產生衍生自 DomainContext 類別的 CustomerDomainContext 類別。此類別有一個名稱為 GetCustomersQuery,且對應到網域服務中建立之查詢方法的方法。

    • 針對網域服務所公開的實體,會產生衍生自 Entity 類別的 Customer 類別。用戶端專案中的 Customer 實體類別會符合伺服器上的 Customer 實體。

若要將資料顯示在 Silverlight 用戶端

  1. 開啟 MainPage.xaml。

  2. DataGrid 控制項從左側的 [工具箱] 拖曳到 XAML 檢視的 Grid 項目中。

    從 [工具箱] 拖曳 [DataGrid] 控制項會自動加入命名空間 using System.Windows.Controls 陳述式和 System.Windows.Controls.Data 組件的參考。

    Caution注意:
    如果您加入 DataGrid 時,沒有從 [工具箱] 拖曳它,則必須將 System.Windows.Controls.Data 組件的參考加入至用戶端珠按,並在程式碼後置檔案中手動使用陳述式。
  3. AutoGeneratedColums 的值變更為 True、將 DataGrid 項目命名為 CustomerGrid,然後調整高度和寬度屬性,如以下 XAML 中所示。

    <UserControl 
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
        x:Class="RIAServicesExample.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
          <data:DataGrid Name="CustomerGrid"></data:DataGrid>
        </Grid>
    </UserControl>
    
  4. 開啟 MainPage.xaml 的程式碼後置檔案。

  5. 加入 using (C#) 或 Imports (Visual Basic) 兩個陳述式:using RIAServicesExample.Web;using System.ServiceModel.DomainServices.Client;

    RIAServicesExample.Web 命名空間是 RIAServicesExample.Web.g.cs (或 RIAServicesExample.Web.g.vb) 中,包含用戶端專案所產生之程式碼的命名空間。

  6. 若要具現化 CustomerDomainContext,請在 MainPage 類別中加入這行程式碼 private CustomerDomainContext _customerContext = new CustomerDomainContext();

    Imports System.ServiceModel.DomainServices.Client
    Imports RIAServicesExample.Web
    
    Partial Public Class MainPage
        Inherits UserControl
    
        Private _customerContext As New CustomerDomainContext
    
        Public Sub New()
            InitializeComponent()
    
            Dim loadOp = Me._customerContext.Load(Me._customerContext.GetCustomersQuery())
            CustomerGrid.ItemsSource = loadOp.Entities
        End Sub
    
    End Class
    
    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.ServiceModel.DomainServices.Client;
    
    namespace RIAServicesExample
    {
        public partial class MainPage : UserControl
        {
            private CustomerDomainContext _customerContext = new CustomerDomainContext();
    
            public MainPage()
            {
                InitializeComponent();
    
                LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
                CustomerGrid.ItemsSource = loadOp.Entities;
            }
        }
    }
    
  7. 使用 LoadOperation: LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery()); 呼叫 GetCustomersQuery 方法來擷取客戶實體。

  8. 使用 CustomerGrid.ItemsSource = loadOp.Entities;,將載入的實體繫結至 DataGrid

    總而言之,MainPage.xaml.cs 檔案現在應該包含下列程式碼:

    //Namespaces added
    using RIAServicesExample.Web;
    using System.ServiceModel.DomainServices.Client;
    
    
    namespace RIAServicesExample
    {
        public partial class MainPage : UserControl
        {
            private CustomerDomainContext _customerContext = new CustomerDomainContext();
            public MainPage()
            {
                InitializeComponent();
    
                LoadOperation<Customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());
                CustomerGrid.ItemsSource = loadOp.Entities;
            }
        }
    }
    
  9. 執行 (F5) 應用程式。

    您應該會看到與下列相似的資料格線。

    RIA_ResultsGrid

後續步驟

本逐步解說僅顯示建立專案以及從網域服務擷取未篩選之資料的基本步驟。以下為學習其他功能的一些建議: