Silverlight WCF RIA服务(九)Domain Service 2

演练:添加查询方法
查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。
当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的查询。这个简单的查询方法检索实体的所有数据。这个演练将描述如何添加一个用参数值来过滤结果的复杂查询方法。还描述了如何添加一个返回单个实体和一个实体集合的查询。
添加一个接受参数并返回单一实体的查询方法

  1. 打开我们第三节中创建的RIAServicesExample解决方案。
  2. 在服务端,打开从Customer表公开数据的domain Services 类。这个类应该叫做CustmerDomainService。
  3. 添加一个查询方法,这个方法接受一个整数类型的参数并返回符合Customer ID的Customer实体。 如果返回单一实体的方法包含Query属性,必须设置IsComposable为false. 用户不能从客户端指定其他的查询操作。如果这个查询方法满足了作为查询所期望的签名,我们就不必使用[Query]属性。返回值必须是任何实体对象的单一实例。
            //自定义添加的方法 手动添加 添加一个接受参数并返回单一实体的查询方法
            [Query(IsComposable = false)]
            public Customers GetCustomersByID(string customerID)
            {
                return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);
            }

     




添加一个接受一个参数并返回一个实体集合的查询方法

  1. 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
  2. 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。


        //添加一个接受一个参数返回一个实体集合的查询方法
        public IQueryable<Customers> GetCustomersByLastNameLetter(string startingLastNameLetter)
        {
            return this.ObjectContext.Customers.Where(c => c.ContactName.StartsWith(startingLastNameLetter) == true);
        }

 


在客户端显示这些查询的结果

  1. 在客户端打开MainPage.xaml文件。
  2. 添加两个TextBox控件和两个Button控件,这样用过就可以通过ID或名的首字母来过滤。下面的xaml代码显示了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
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    <?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">
           <grid.columndefinitions>
               <columndefinition></columndefinition>
               <columndefinition></columndefinition>
           </grid.columndefinitions>
           <grid.rowdefinitions>
               <rowdefinition height="25"></rowdefinition>
               <rowdefinition></rowdefinition>
           </grid.rowdefinitions>
           <stackpanel column="0" row="0" orientation="Horizontal">
               <textblock text="search by id: "></textblock>
               <textbox name="IDValue" width="50"></textbox>
               <BUTTON name=IDButton type=submit click="IDButton_Click" content="Submit"></BUTTON>
           </stackpanel>
           <stackpanel column="1" row="0" orientation="Horizontal">
               <textblock text="search by name: "></textblock>
               <textbox name="LetterValue" width="30"></textbox>
               <BUTTON name=LetterButton type=submit click="LetterButton_Click" content="Submit"></BUTTON>
           </stackpanel>
         <?xml:namespace prefix =data ns = "http://www.google.com/2005/gml/data" /><data:datagrid name="CustomerGrid" column="0" row="1" columnspan="2"></data:datagrid>
       </grid>
    </usercontrol>

     
    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="ExampleSilverlightApp.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <!--<StackPanel Orientation="Horizontal">
                <sdk:DataGrid x:Name="ProductsGrid">            
                </sdk:DataGrid>
                
                <sdk:DataGrid x:Name="CustomersGrid">                
                </sdk:DataGrid>
            </StackPanel>-->
            
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="25"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
                <TextBlock Text="Search By ID:"></TextBlock>
                <TextBox x:Name="IDValue" Width="50"/>
                <Button x:Name="IDButton" Click="IDButton_Click" Content="Submit"/>
            </StackPanel>
            <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                <TextBlock Text="Search By Name:"></TextBlock>
                <TextBox x:Name="LetterValue" Width="30"/>
                <Button x:Name="LetterButton" Click="LetterButton_Click" Content="Submit"/>
            </StackPanel>
            <sdk:DataGrid Grid.Row="1" Grid.ColumnSpan="2" x:Name="CustomerGrid"/>
        </Grid>
    </UserControl>
    



  3. 打开MainPage.xaml的代码文件。

  4. 添加代码来根据用户的输入来检索数据。
    ?
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    public partial class MainPage : UserControl
    {
       private CustomerDomainContext _customerContext =new CustomerDomainContext();
       public MainPage()
       {
           InitializeComponent();
       }
       private void LetterButton_Click(object sender, RoutedEventArgs e)
       {
           IDButton.IsEnabled =false;
           LetterButton.IsEnabled =false;
           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.GetCustomersByLastNameLetterQuery(LetterValue.Text), CustomerLoadedCallback, null);
           CustomerGrid.ItemsSource = loadOp.Entities;
       }
       private void IDButton_Click(object sender, RoutedEventArgs e)
       {
           IDButton.IsEnabled =false;
           LetterButton.IsEnabled =false;
           LoadOperation<customer> loadOp =this._customerContext.Load(this._customerContext.GetCustomersByIDQuery(int.Parse(IDValue.Text)), CustomerLoadedCallback, null);
           CustomerGrid.ItemsSource = loadOp.Entities;
       }
       void CustomerLoadedCallback(LoadOperation<customer> loadOperation)
       {
           IDButton.IsEnabled =true;
           LetterButton.IsEnabled =true;
       }
    }


    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 AdvertureWorksClassLibrary.Web;
    using System.ServiceModel.DomainServices.Client;
    
    namespace ExampleSilverlightApp
    {
        public partial class MainPage : UserControl
        {
            private CustomerDomainContext _customerDomainContext = new CustomerDomainContext();
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void IDButton_Click(object sender, RoutedEventArgs e)
            {
                IDButton.IsEnabled = false;
                LetterButton.IsEnabled = false;
                //查询的第一种写法:
                //this.CustomerGrid.ItemsSource = _customerDomainContext.Customers;
                //_customerDomainContext.Load(_customerDomainContext.GetCustomersByIDQuery(IDValue.Text));
                
                //查询的第二种写法:
                LoadOperation<Customers> loadOp = this._customerDomainContext.Load(
                    this._customerDomainContext.GetCustomersByIDQuery(IDValue.Text)
                    );
                CustomerGrid.ItemsSource = loadOp.Entities;
    
                ReturnCanClick();
            }
    
            private void LetterButton_Click(object sender, RoutedEventArgs e)
            {
                IDButton.IsEnabled = false;
                LetterButton.IsEnabled = false;
                //查询的第一种方法
                //this.CustomerGrid.ItemsSource = _customerDomainContext.Customers;
                //_customerDomainContext.Load(_customerDomainContext.GetCustomersByLastNameLetterQuery(LetterValue.Text));
    
                //查询的第二种方法
                LoadOperation<Customers> loadOp = this._customerDomainContext.Load(
                    this._customerDomainContext.GetCustomersByLastNameLetterQuery(LetterValue.Text)
                    );
                CustomerGrid.ItemsSource = loadOp.Entities;
    
                ReturnCanClick();
            }
    
            private void ReturnCanClick()
            {
                this.IDButton.IsEnabled = true;
                this.LetterButton.IsEnabled = true;
            }
        }
    }
    

     

  5. 运行解决方案。将会看到如下结果

 

源代码地址: http://download.csdn.net/detail/eric_k1m/5718757

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值