演练:添加查询方法
查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。
当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的查询。这个简单的查询方法检索实体的所有数据。这个演练将描述如何添加一个用参数值来过滤结果的复杂查询方法。还描述了如何添加一个返回单个实体和一个实体集合的查询。
添加一个接受参数并返回单一实体的查询方法
- 打开我们第三节中创建的RIAServicesExample解决方案。
- 在服务端,打开从Customer表公开数据的domain Services 类。这个类应该叫做CustmerDomainService。
- 添加一个查询方法,这个方法接受一个整数类型的参数并返回符合Customer ID的Customer实体。 如果返回单一实体的方法包含Query属性,必须设置IsComposable为false. 用户不能从客户端指定其他的查询操作。如果这个查询方法满足了作为查询所期望的签名,我们就不必使用[Query]属性。返回值必须是任何实体对象的单一实例。12345678910111213141516171819202122
[Query(IsComposable=
false
)]
public
Customer GetCustomersByID(
int
customerID)
{
return
this
.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);
}
- 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
- 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。123456789101112131415161718
public
IQueryable<CUSTOMER> GetCustomersByLastNameLetter(
string
startingLastNameLetter)
{
return
this
.ObjectContext.Customers.Where(c => c.LastName.StartsWith(startingLastNameLetter) ==
true
);
}
- 在客户端打开MainPage.xaml文件。
- 添加两个TextBox控件和两个Button控件,这样用过就可以通过ID或名的首字母来过滤。下面的xaml代码显示了DataGrid的完整布局。123456789101112131415161718192021222324252627282930313233343536373839@H_849_404@ 40414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788@H_15_502@ 8990919293949596979899100101102
<?
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
>
</
grid.columndeFinitions
>
<
grid.rowdeFinitions
>
</
grid.rowdeFinitions
>
<
stackpanel
column
=
"0"
row
=
"0"
orientation
=
"Horizontal"
>
<
textblock
text
=
"search by id: "
></
textblock
>
<
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
>
<
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
>
- 打开MainPage.xaml的代码文件。
- 添加代码来根据用户的输入来检索数据。123456789101112131415161718192021222324252627282930313233343536373839@H_849_404@ 40414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788@H_15_502@ 8990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
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)),
null
);
CustomerGrid.ItemsSource = loadOp.Entities;
}
void
CustomerLoadedCallback(LoadOperation<customer> loadOperation)
{
IDButton.IsEnabled =
true
;
LetterButton.IsEnabled =
true
;
}
}
- 运行解决方案。将会看到如下结果
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。