基于 Silverlight的精简框架之版本升级及使用

      最近研究了下RIA Services+EF自动生成Silverlight context代码非常方便,但是只有使用EF/Linqtosql + LinqToEntitiesDomainService的方式才能同Silverlight结合的很好,且可控性不是很好,在领域模型耦合度还是比较高,难以同传统的三层架构无缝结合。
      然后回顾了自己的EasySL框架,究其量也只是个轻量级的DomainService,所以提取出核心的数据传输服务,作为独立的版本发布,如果不适应RIA Services的开发方式,可以借以了解下EasySL.


-----------------------------------------------------------------------------------------------------------------------

  • 你可以在http://www.codeplex.com/EasySL下载到最新的代码;
  • 最新的版本在2.0文件夹里,已经精简到只有两个项目EasySL.Core 和 EasySL.Control,里面还包括了一个Sample文件夹,主要是提供了一个DataGrid+DataForm基于Easy实现CRUD及分页的示例;
  • 此版本已经去掉大部分控件,remoting分布式部署,只保留数据交互核心模块。

-----------------------------------------------------------------------------------------------------------------------

 

Usage:

 -----------------------------------------------------------------------------------------------------------------------

1. 在你的web项目中引用EasySL.Core,在你的web项目里新建Service.ashx,作为data/domain service用

1 public class Service : DataServiceHandler<EasySL.Data.DAO>

-----------------------------------------------------------------------------------------------------------------------

2. 建立entity项目,YourProject.Entity和YourProject.EntitySL,2个项目共享同份代码

代码
 1 public class Product : INotifyPropertyChanged
 2 {
 3     private string name;
 4 
 5     public string Name
 6     {
 7         get { return name; }
 8         set {
 9             if (string.IsNullOrEmpty(value))
10                 throw new Exception("Name cant be null");
11 
12             name = value; 
13             NotifyPropertyChanged("Name");
14         }
15     }
16 }


-----------------------------------------------------------------------------------------------------------------------

3.建立data项目,YourProject.Data(DAL),建立DAO数据实体,对应web项目中的DataServiceHandler<EasySL.Data.DAO>

代码
 1 public class DAO
 2 {
 3     public int GetProductCount()
 4 
 5     public List<Product> GetProducts(int pageIndex, int pageSize)
 6 
 7     public void SaveOrAddProduct(Product product)
 8 
 9     public void DeleteProduct(int id)
10 }

 

-----------------------------------------------------------------------------------------------------------------------

4.在你的silverlight项目中引用EasySL.Core.SL

单任务:
代码
 1 Task task = new Task();
 2 
 3         task.Begin += (t) =>
 4         {
 5             t.MethodName = "GetProducts";
 6             t.SetParameter("pageIndex", pager.PageIndex);
 7             t.SetParameter("pageSize", pager.PageSize);
 8             t.ReturnType = typeof(List<Product>);
 9 
10             this.loading.Show("Get products...................");
11         };
12 
13         task.End += res =>
14         {
15             this.products = res.Data as List<Product>;
16 
17             this.dg.ItemsSource = this.df.ItemsSource = this.products;
18             this.loading.Hide();
19             });
20         };
21         task.Start();

多任务(串行执行):

代码
 1 TaskList taskList = new TaskList();
 2             
 3         Task task1 = new Task();
 4         task1.Begin += ...
 5         task1.End += ...
 6 
 7         Task task2 = new Task();
 8         task2.Begin += ...
 9         task2.End += ...
10             
11         taskList.Add(task1);
12         taskList.Add(task2);
13         taskList.Start();   


-----------------------------------------------------------------------------------------------------------------------

 5. Task全局异常处理
 如果你需要捕获服务端异常,在page.xaml页面文件里注册Requestor:
 
代码
1 EasySL.Core.Requestor.OnResponse += response =>
2                 {
3                     if (response.Status == ResponseStatus.ServiceException)
4                         EasySL.Controls.Window.Alert(response.Message);
5                 };



Demo: http://guozili.25u.com/2009/#6
原文地址:https://www.cnblogs.com/guozili/p/1818351.html