演练:添加查询方法
查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。
当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的查询。这个简单的查询方法检索实体的所有数据。这个演练将描述如何添加一个用参数值来过滤结果的复杂查询方法。还描述了如何添加一个返回单个实体和一个实体集合的查询。
添加一个接受参数并返回单一实体的查询方法
- 打开我们第三节中创建的RIAServicesExample解决方案。
- 在服务端,打开从Customer表公开数据的domain Services 类。这个类应该叫做CustmerDomainService。
- 添加一个查询方法,这个方法接受一个整数类型的参数并返回符合Customer ID的Customer实体。 如果返回单一实体的方法包含Query属性,必须设置IsComposable为false. 用户不能从客户端指定其他的查询操作。如果这个查询方法满足了作为查询所期望的签名,我们就不必使用[Query]属性。返回值必须是任何实体对象的单一实例。
//自定义添加的方法 手动添加 添加一个接受参数并返回单一实体的查询方法 [Query(IsComposable = false)] public Customers GetCustomersByID(string customerID) { return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID); }
添加一个接受一个参数并返回一个实体集合的查询方法
- 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
- 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。
//添加一个接受一个参数返回一个实体集合的查询方法
public IQueryable<Customers> GetCustomersByLastNameLetter(string startingLastNameLetter)
{
return this.ObjectContext.Customers.Where(c => c.ContactName.StartsWith(startingLastNameLetter) == true);
}
在客户端显示这些查询的结果
- 在客户端打开MainPage.xaml文件。
- 添加两个TextBox控件和两个Button控件,这样用过就可以通过ID或名的首字母来过滤。下面的xaml代码显示了DataGrid的完整布局。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102<?
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
>
<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>
- 打开MainPage.xaml的代码文件。
- 添加代码来根据用户的输入来检索数据。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130public
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; } } }
- 运行解决方案。将会看到如下结果