Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1105940
  • 博文数量: 141
  • 博客积分: 3161
  • 博客等级: 中校
  • 技术积分: 3011
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-27 14:53
文章存档

2012年(28)

2011年(113)

分类: 嵌入式

2012-01-05 09:15:35

2.4. 企业架构模式实战

还是继续本章开始的那个电子商务的例子:通过给定产品的分类获取所有的产品。下面我们要做的就产品展示。

 

要显示产品数据,最快速的做法就是建立一个ASP.NET的站点项目,新加一个页面,然后在页面上拖放GridView等类似的数据绑定控件,双击打开页面背后的类文件,敲上几行数据绑定的代码,这件事情就搞定了。如图2-10所示。

 

 

代码如下所示:

 

  1. private void BindProductsByCategoryId(intcategoryId)
  2. {
  3.     var productService = new ProductService();
  4.     this.gvProductList.DataSource = productService.GetAllProductsFrom(categoryId);
  5.     this.gvProductList.DataBind();
  6. }
 

    这种做法目前没有任何问题,值得提倡,简单就是美,够用就行,在业务和功能不复杂的情况下,它确实是可取方案!

 

    但是如果需要在另一个页面上也显示商品列表,那么上面的所有操作几乎要重新做一遍:拖控件,写绑定代码。这里隐含的意思就是:功能相同或者相近的代码在多个地方出现。

 

    在例子中,每次要想知道数据是否读取正确,只能通过运行程序来了解,缺乏可测试性。如果逻辑再复杂一点,页面再多一些,工作量就大了。

相信大家已经明白:此处,即将采用MVP模式。当然,这里没有给出足够的理由来说服大家为什么要采用这种模式,后续章节将会补上。

 

    首先看看MVP模式的结构,如图2-11所示。

 

 

    对于图2-11

q  View用来显示和收集用户输入的数据,并且把用户的请求传递给Presenter

q  Presenter充当一个中介者,向View隐藏了所有复杂的逻辑,只是暴露了一个简单的APIView调用,并且只要传入的对象实现了View的接口,Presenter都会将之看成View

 

        Model业务对象,接受Presenter传递过来的业务请求,真正处理业务逻辑是Model对象。

 

    按照上面的结构,本例中MVP模式的实现结构如图2-12所示。   

    

 

    基于上述分析,MVP模式在Visual Studio中的实现如图2-13所示。

 

 

    IProductView接口的定义如下:

 

  1. namespace AgileSharp.Chapter2.WebUI.Interface
  2. {
  3.     public interface IProductView
  4.     {
  5.         void DisplayProducts(List<Product> data);
  6.     }
  7. }

 

    不管数据从哪里来,通过什么条件获取,这个接口的作用就是获取数据,然后显示。Default.aspx实现这个接口,如下:

 

  1. public partial class _Default :System.Web.UI.Page,IProductView
  2. {
  3.     public void DisplayProducts(List<Product> data)
  4.       {
  5.         this.gvProductList.DataSource = data;
  6.         this.gvProductList.DataBind();
  7.       }
  8. }

    如之前所示,ProductPresenter就是中介者,它传递请求,返回结果。如下所示:

 

  1. namespace AgileSharp.Chapter2.WebUI.Presenter
  2. {
  3.     public class ProductPresenter
  4.      {
  5.     private ProductServiceproductService = null;
  6.         privateIProductView view = null;

  7.     publicProductPresenter()
  8.         {
  9.         productService = new ProductService();
  10.         }

  11.     public void Init(IProductView view)
  12.         {
  13.         this.view = view;
  14.         }

  15.     public void LoadProductsByCategory(intcategoryId)
  16.         {
  17.         var data = productService.GetAllProductsFrom(categoryId);
  18.         if (data != null &&data.Count> 0)
  19.              {
  20.         this.view.DisplayProducts(data);
  21.              }
  22.         }
  23.     }
  24. }

    最后要做的就是将PresenterView联系起来,如下:

 

  1. public partial class _Default :System.Web.UI.Page,IProductView
  2. {
  3.     private ProductPresenter presenter = null;

  4.     protected override void OnInit(EventArgs e)
  5.       {
  6.         base.OnInit(e);
  7.         presenter = new ProductPresenter();
  8.       }

  9.       protected void Page_Load(object sender,EventArgs e)
  10.       {
  11.         if (!this.IsPostBack)
  12.               {
  13.         presenter.Init(this);
  14.               }
  15.         }

  16.     public void DisplayProducts(List<Product> data)
  17.       {
  18.         this.gvProductList.DataSource = data;
  19.         this.gvProductList.DataBind();
  20.       }

  21.       protected void ddlCategoryList_SelectedIndexChanged(object sender,EventArgs e)
  22.       {
  23.         int category = (int)this.gvProductList.SelectedValue;
  24.         presenter.LoadProductsByCategory(category);
  25.       }
  26. }

    上述代码应该很清楚,这里就不再赘述,在第八章将会详细的讨论MVP模式等表现模式!

注意  完整的代码示例,可以参看AgileSharp.Chapter2文件夹。

 

  当当地址:

  京东地址:

  卓越地址:

  淘宝地址:

阅读(2633) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

hfeng19852012-02-17 09:48:01

好文章啊