演练二:添加查询方法(MSDN译文)
2010-01-10 19:40
204 查看
上一节的演练仅仅是揭示了创建一个项目通过Domain Service来检索数据的基本步骤而已,未加深入学习如何建立更多复杂的查询方法,诸如接收一个参数请求来筛选数据。
做些查询一个数据源的方法被命名为查询方法。在WCF RIA Services中,查询方法必须以被定义的方式让架构认可这些方法。此外,总是返回一个单一实体的查询与潜在返回一个实体以上的查询是被不同定义的。有关合法查询方法的签名的更多信息,可看Domain Services
当你在Add New Domain Service Class对话框中建立一个新的domain service类并且指定一个实体时,RIA Services架构为由这个服务暴露的每个实体自动创建一个简单的查询方法。这个简单的查询方法检索这个实体所有的记录。而这一章演练阐述了如何添加一个新的查询方法来执行更复杂的类似通过一个参数值来进行筛选的情景,以下是如何添加返回单一实体和返回一个实体集的查询。
添加一个查询方法,它接收一个参数并返回一个单一实体
打开之前的RIAServicesExample解决方案。
在服务端项目中,打开那暴露Customer表数据的domain services类,前一讲中此类已命名为CustomerDomainService。
添加一个查询方法,其接受一个整型参数并返回匹配customer ID的Customer实体。
如果返回单一实体的查询方法包含QueryAttribute属性,则必须设置IsComposable特性为false。用户不能从客户端指定其他查询操作。如果查询方法与查询的预期签名相匹配,你不得不应用QueryAttribute属性。返回值必须是一个实体对象的单一实例。
[Query(IsComposable=false)]
public Customer GetCustomersByID(int customerID)
{
return this.ObjectContext.Customers.SingleOrDefault(c=>c.CustomerID==customerID);
}
添加一个查询方法,它接收一个参数并返回一个实体集
这次我们在domain service类中添加的这个查询方法,接受一个字符串参数并返回以字母姓氏开头至结尾名字的任何customers记录。这个方法能够返回一个IQueryable<>对象,因为用户可能需要从客户端提供额外的查询操作。
public IQueryable<Customer> GetCustomersByLastNameLetter(string
startingLastNameLetter)<BR>{<BR>return
this.ObjectContext.Customers.Where(c=>c.LastName.StartsWith(startingLastNameLetter)==true);<BR>}
在客户端项目中这些查询方法的显示结果
MainPage.xaml
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="RIAServicesExample.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">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
<TextBlock Text="search by id: " ></TextBlock>
<TextBox Name="IDValue" Width="50" ></TextBox>
<Button Name="IDButton" Click="IDButton_Click" Content="Submit"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
<TextBlock Text="search by name: "></TextBlock>
<TextBox Name="LetterValue" Width="30"></TextBox>
<Button Name="LetterButton" Click="LetterButton_Click" Content="Submit"></Button>
</StackPanel> <data:DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="CustomerGrid"></data:DataGrid>
</Grid>
</UserControl>
MainPage.xaml.cs
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<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;
} } 最后运行这个编译后的应用程序。
做些查询一个数据源的方法被命名为查询方法。在WCF RIA Services中,查询方法必须以被定义的方式让架构认可这些方法。此外,总是返回一个单一实体的查询与潜在返回一个实体以上的查询是被不同定义的。有关合法查询方法的签名的更多信息,可看Domain Services
当你在Add New Domain Service Class对话框中建立一个新的domain service类并且指定一个实体时,RIA Services架构为由这个服务暴露的每个实体自动创建一个简单的查询方法。这个简单的查询方法检索这个实体所有的记录。而这一章演练阐述了如何添加一个新的查询方法来执行更复杂的类似通过一个参数值来进行筛选的情景,以下是如何添加返回单一实体和返回一个实体集的查询。
添加一个查询方法,它接收一个参数并返回一个单一实体
打开之前的RIAServicesExample解决方案。
在服务端项目中,打开那暴露Customer表数据的domain services类,前一讲中此类已命名为CustomerDomainService。
添加一个查询方法,其接受一个整型参数并返回匹配customer ID的Customer实体。
如果返回单一实体的查询方法包含QueryAttribute属性,则必须设置IsComposable特性为false。用户不能从客户端指定其他查询操作。如果查询方法与查询的预期签名相匹配,你不得不应用QueryAttribute属性。返回值必须是一个实体对象的单一实例。
[Query(IsComposable=false)]
public Customer GetCustomersByID(int customerID)
{
return this.ObjectContext.Customers.SingleOrDefault(c=>c.CustomerID==customerID);
}
添加一个查询方法,它接收一个参数并返回一个实体集
这次我们在domain service类中添加的这个查询方法,接受一个字符串参数并返回以字母姓氏开头至结尾名字的任何customers记录。这个方法能够返回一个IQueryable<>对象,因为用户可能需要从客户端提供额外的查询操作。
public IQueryable<Customer> GetCustomersByLastNameLetter(string
startingLastNameLetter)<BR>{<BR>return
this.ObjectContext.Customers.Where(c=>c.LastName.StartsWith(startingLastNameLetter)==true);<BR>}
在客户端项目中这些查询方法的显示结果
MainPage.xaml
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="RIAServicesExample.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">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
<TextBlock Text="search by id: " ></TextBlock>
<TextBox Name="IDValue" Width="50" ></TextBox>
<Button Name="IDButton" Click="IDButton_Click" Content="Submit"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
<TextBlock Text="search by name: "></TextBlock>
<TextBox Name="LetterValue" Width="30"></TextBox>
<Button Name="LetterButton" Click="LetterButton_Click" Content="Submit"></Button>
</StackPanel> <data:DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="CustomerGrid"></data:DataGrid>
</Grid>
</UserControl>
MainPage.xaml.cs
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<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;
} } 最后运行这个编译后的应用程序。
相关文章推荐
- 添加,查询,修改,删除测试方法及常见bug说明
- ArcEngine查询、添加、删除要数的方法
- MySQL中给自定义的字段查询结果添加排名的方法
- 实现CopyToDataTable 的T不为DataRow的查询方法(和MSDN上的方法一样)
- 4.解决多表连接中(左连和等值)因添加新的报表导致查询结果有问题的方法
- mysql简单实现查询结果添加序列号的方法
- mysql简单实现查询结果添加序列号的方法
- Android编程操作联系人的方法(查询,获取,添加等)
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)
- javaEE数据库查询数据时,bean对象需要添加属性的set、get方法,否则查找不到
- sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)
- 添加、删除、修改、查询的四个基本方法
- 如何在查询结果中添加自动编号的方法
- java字符串截取、替换、删除、添加、查询等常用方法
- MySQL添加中文查询结果在乱码的解决方法
- 为JS数组/Json/Xml添加折半查询方法
- 毕业设计(二十一)---后台管理添加查询方法,按照xxx,升序/降序,每页显示X条记录
- 多库查询 sp_addlinkedserver使用方法(添加链接服务器)(转)片段整理
- ADF InputListofValue 搜索帮助上添加模糊查询字段的方法
- eclipse插件Maven添加依赖查询无结果的解决方法(Select Dependency doesn't work)