Silverlight WCF RIA服务(九)Domain Service 2

Silverlight WCF RIA服务(九)Domain Service 2,第1张

概述演练:添加查询方法 查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。 当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的

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

打开我们第三节中创建的RIAServicesExample解决方案。
在服务端,打开从Customer表公开数据的domain Services 类。这个类应该叫做CustmerDomainService。
添加一个查询方法,这个方法接受一个整数类型的参数并返回符合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);       }

 




 

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

打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。
添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个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);       }


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

 

在客户端打开MainPage.xaml文件。
添加两个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 @H_517_404@ 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 @H_404_482@ 78 79 80 81 82 83 84 85 86 @H_419_500@ 87 @H_829_502@ 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>   @H_517_404@             </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> @H_404_482@               </stackpanel>           @H_419_500@   @H_829_502@         <?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的代码文件。

添加代码来根据用户的输入来检索数据。
?
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 @H_517_404@ 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 @H_404_482@ 78 79 80 81 82 83 84 85 86 @H_419_500@ 87 @H_829_502@ 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)   @H_517_404@         {               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; @H_404_482@               Letterbutton.IsEnabled = false;               LoadOperation<customer> loadOp = this._customerContext.Load(this._customerContext.GetCustomersByIDquery(int.Parse(IDValue.Text)),null);   @H_419_500@   @H_829_502@           CustomerGrID.ItemsSource = loadOp.EntitIEs;           }                   voID CustomerLoadedCallback(LoadOperation<customer> loadOperation)           {               IDbutton.IsEnabled = true;               Letterbutton.IsEnabled = true;           }               }


 

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

总结

以上是内存溢出为你收集整理的Silverlight WCF RIA服务(九)Domain Service 2全部内容,希望文章能够帮你解决Silverlight WCF RIA服务(九)Domain Service 2所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1065772.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存