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]属性。返回值必须是任何实体对象的单一实例。
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [Query(IsComposable=false)]
     
     
     
    public Customer GetCustomersByID(int customerID)
     
     
     
    {
     
     
     
        return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);
     
     
     
    }

     




 

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

  1. 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
  2. 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public IQueryable<CUSTOMER> GetCustomersByLastNameLetter(string startingLastNameLetter)
     
     
     
    {
     
     
     
        return this.ObjectContext.Customers.Where(c => c.LastName.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.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">
     
     
     
            <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>

     

  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;
     
     
     
        }
     
     
     
     
     
     
     
    }


     

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值