Silverlight客户端访问
1. 首先Project Link到服务端项目,Build服务端项目是在客户端项目的Generated_Code和其他一些目录下会生成相关的代码
2. 使用代码或XAML(DomainDataSource )访问服务
xmlns:domain="clr-namespace:RIA.Web.Services"
Title="Demo Page" Style="{StaticResource PageStyle}"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<riaControls:DomainDataSource Name="DataSource" AutoLoad="True"
QueryName="GetCustomers">
<riaControls:DomainDataSource.DomainContext>
<domain:NorthWindContext/>
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<sdk:DataGrid Name="dataGrid1" Grid.Row="0" ItemsSource="{Binding ElementName=DataSource, Path=Data}" />
<sdk:DataGrid Name="dataGrid2" Grid.Row="1"/>
</Grid>
NorthWindContext context = new NorthWindContext();
EntityQuery<Customer> query = context.GetCustomersQuery();
///Load all
//context.Load<Customer>(query);
//this.dataGrid1.ItemsSource = context.Customers;
var q = from c in query where c.City == "london" orderby c.CustomerID select c;
context.Load<Customer>(q);
this.dataGrid2.ItemsSource = context.Customers;
查看HTTP协议可以看到发到服务端的请求如下:
GET /ClientBin/RIA-Web-Services-NorthWindService.svc/binary/GetCustomers
GET /ClientBin/RIA-Web-Services-NorthWindService.svc/binary/GetCustomers?$where=(it.City%253d%253d%2522london%2522)&$orderby=it.CustomerID HTTP/1.1
可以看到整个的方式和WCF Data Service基本一致
注:DomainDataSource包含在 System.Windows.Controls.DomainServices组件中
服务端Domain Service方法
服务端服务类中的方法遵循如下的约定
方法前缀 |
标注属性 |
说明 |
(Any) |
[Query()] |
A method that returns data without any side effects. The usual approach is to prefix with Get, but any prefix is fine as long as the function returns an instance of an entity T, an IEnumerable<T>, or an IQueryable<T>. |
Insert, Add, Create |
[Insert()] |
An operation that inserts a single entity into the data store. The method takes the entity as a parameter. |
Update, Change, Modify |
[Update()] |
An operation that updates a single entity in the data store. The method takes the entity as a parameter. |
Delete, Remove |
[Delete()] |
An operation that deletes an entity in the data store. The method takes the entity as a parameter. |
(Any) |
[Invoke()] |
A business method that must be executed without tracking or deferred execution. It may or may not have side effects. Use only when one of the other method types can’t be used. |
(Any) |
[Update(UsingCustomMethod=true)] |
标注UsingCustomMethod=true属性,执行特定更新的操作,如需要手动进行级联更新的情况 参数是实体类,在SubmitChanges调用时,自动调用这个更新实体的函数 |
- |
[Ignore()] |
方法尽管符合约定如UpdateEmployee,但不要生成客户端的桩 |
以上的说明都是在服务端类中可以编写的方法,在这个类中你可以进行业务逻辑的验证和处理,数据的更新等等各种操作.
例子
http://dskit.codeplex.com 上的RIA RIA.Web 项目
运行效果如下图:
包括了数据的列表显示,数据的编辑和Domain Service方法,具体有:
DomainDataSource 的使用:过滤、分页、排序、分组等
DataForm 和DomainDataSource结合实现数据的增删改
手工的调用服务端方法
Sliverlight中,如果对于比较大的应用,为了更清晰的分层,可以考虑使用MVVP模式
特别注意:Sliverlight中调用服务端的代码,很多情况下都是异步的[具体的原因和浏览器有关,详细解释参考MSDN],因此使用方法返回的结果需要注意