使用RIA服务向服务器提交变更的数据
实例:使用DomainDataSource控件向服务器提交变更数据
有两种方法实现:一是在XMAL文件的后置代码中调用该方法;二是将按钮(按钮单击后向服务器提交数据)的Command属性绑定到DomainDataSource控件的SubmitChangesCommand属性上;
第二种方法的实现代码:
<Button Content="Save Changes" Width="100" Height="30"
Command="{Binding SubmitChangesCommand,
ElementName=productDomainDataSource}" />
实例:通过域上下文向服务器提交变更数据
域上下文类有一个SubmitChanges方法,可以将客户端的变更传递到服务器:
1)向视图中添加按钮,其Click事件在后置代码中处理:
<Button Content="Save Changes" Click="SaveChangesButton_Click"
Width="100" Height="30" />
2)为了确保查询与更新数据使用的上下文是同一对象,需要在后置代码中定义一个私有变量用于存储上下文对象,然后在视图的构造器中将此上下文对象实例化,这样就可以统一使用这一对象了;
private ProductContext _context = null;
public ProductListView()
{
InitializeComponent();
_context = new ProductContext();
EntityQuery<Product> qry = _context.GetProductsQuery();
LoadOperation<Product> operation = _context.Load(qry);
productDataGrid.ItemsSource = operation.Entities;
}
3)在Button控件的Click事件处理方法里调用上下文的SubmitChanges方法:
private void SaveChangesButton_Click(object sender,
System.Windows.RoutedEventArgs e)
{
SubmitOperation submitOperation = _context.SubmitChanges();
}
处理提交数据过程中的错误
如果采用DomainDataSource控件,需要调用SubmittedChanged事件处理提交数据过程中的错误,如果使用域上下文来提交数据,则需要处理SumbitOperation对象的Complete事件来处理错误。
采用DomainDataSource控件处理错误的案例
private void productDomainDataSource_SubmittedChanges(object sender,
SubmittedChangesEventArgs e)
{
if (e.HasError)
{
MessageBox.Show(e.Error.Message);
e.MarkErrorAsHandled();
}
}
采用域上下文类处理错误的案例
首先要为SumbitOperation对象添加Complete事件的委托:
SubmitOperation submitOperation = _context.SubmitChanges();
submitOperation.Completed += submitOperation_Completed;
再编写submitOperation_Completed处理方法:
private void submitOperation_Completed(object sender, System.EventArgs e)
{
SubmitOperation submitOperation = sender as SubmitOperation;
if (submitOperation.HasError)
{
MessageBox.Show(submitOperation.Error.Message);
submitOperation.MarkErrorAsHandled();
}
}
两种方法捕获的异常都可以强制转换为DomainOperationException类型,并且使用OperationErrorStatus枚举来判断异常的类型,该枚举成员如下(与上一节的内容类似):
示例如下:
if (e.HasError)
{
DomainOperationException error = e.Error as DomainOperationException;
switch (error.Status)
{
case OperationErrorStatus.Conflicts:
// Handle concurrency violations
break;
case OperationErrorStatus.ServerError:
// Handle server errors
break;
case OperationErrorStatus.Unauthorized:
// Handle unauthorized domain operation access
break;
case OperationErrorStatus.ValidationFailed:
// Handle validation rule failures
break;
default:
// Handle other possible statuses
break;
}
}
并发冲突的处理,一般有如下几种方法:
1)弃用更新数据,重新输入原始数据(不太友好);
2)使用用户最后更新的数据覆盖服务器上的值(可能会丢失数据);
3)显示当前版本(用户想要更新的数据)与存储版本(数据库中的数据),让用户自行取舍;
4)尝试自动将变更数据并入;
下面的案例选择了第2种方法:
if (submitOperation.HasError)
{
DomainOperationException error =
submitOperation.Error as DomainOperationException;
if (error.Status == OperationErrorStatus.Conflicts)
{
// Loop through the entities with concurrency violations
foreach (Product product in submitOperation.EntitiesInError)
{
EntityConflict conflictinfo = product.EntityConflict;
Product currentProduct = conflictinfo.CurrentEntity as Product;
Product originalProduct = conflictinfo.OriginalEntity as Product;
Product storeProduct = conflictinfo.StoreEntity as Product;
// Handle any conflicts automatically (if you wish)
// You can get the names of the properties whose value has changed
// from the conflictinfo.PropertyNames property
// Force the user's version to overwrite the server’s version
product.EntityConflict.Resolve();
}
}
}
创建Http请求
可以在Silverlight中直接向服务器请求数据,如下载或上传数据。有两个类可以完成此项工作,一个是WebClient类,WebClient 类提供向 URI 标识的任何本地、Intranet 或 Internet 资源发送数据以及从这些资源接收数据的公共方法。一个是WebRequest类,使用该请求/响应模型的应用程序可以用协议不可知的方式从 Internet 请求数据,在这种方式下,应用程序处理 WebRequest 类的实例,而协议特定的子类则执行请求的具体细节。一般使用WebClient类完成下载或上传的任务,有四个方法可用,DownloadString,UploadString分别用于下载/上传字符串数据,OpenRead、OpenWrite用于流数据的下载与上传。下述代码描述了如何从服务器获取文本内容:
private void StartDownload(string url)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += client_DownloadStringCompleted;
client.DownloadStringAsync(new Uri(url));
}
private void client_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
string result = e.Result;
// Do something with the result
}
对HTTP请求的更多信息参考MSDN:http://msdn.microsoft.com/zh-cn/library/system.net.webclient(v=VS.80).aspx