CAB 与 MVP 模式

CAB 与 MVP 模式

      首先要说明的是CAB 是指MS Composite UI Application Block , MVP 是指 Model-View-Presenter 模式,他是MVC模式的改进版,由Martin Flower提出,在这里将如何把CAB和MVP模式结合的方法记录下来。
      CAB前面已经记录了一些了,虽然都是一些较为粗浅的东西,但我自己也算是刚刚入了门。这里就先说说MVP模式是个什么概念。
      首先MVP是MVC的师弟,同样也具有View和Model,不同的是MVC中的C是Control,而MVP中的P是Presenter,放出一张CAB文档中的示意图,大家大概就能有个了解。

      这似乎看来和MVC并没有多大的不同,MVC中通常会出现的类和接口包括(View,IView,Control,IControl,Model和IModule),
      结构如下(为了结构清晰没有引入依赖注入机制):
      

 
 
public interface IView { void DoSomething(); void WireUp(IModel,IControl); // 更新自身状态 void Update(IModel paramModel); } public interface IControl { void SetModel(IModel paramAuto); void SetView(IView paramView); } public interface IModel { void SetView(IView paramView); void UpdateView(IView paramView); // some property }

 MVP中会出现的类和接口包括(View,IView,Preseter,IPresenter,Module和IModule),
   结构如下(为了结构清晰没有引入依赖注入机制和自动事件挂接机制,故与上图有些许不同):
  

 
 
public interface IView { // 一些事件的定义如果,包含所有你期望从View中Fire的事件 event EventHandler ViewLoad; // all method to update View void BindDataItem(); // just take as an example // properties string Caption // just take as an example { set ; } } public interface IPreseter { // 设置对应的View SetView(IView); // 订阅相应的Iview的事件 void SubscriverToViewEvent() // 处理View中触发的事件 void ViewLoad(); // 更新View的方法 void UpdateView(); } public interface IModle { void SetPersenter(IPreseter paramPreseter); // 调用Persenter,并要求它更新界面的一些方法 void CallPersenterToUpdateView(); }

看完上面的代码,差异是不是慢慢就呈现出来了,在MVP中View根本就不知道Perserter的存在,并且几乎变成了一个不包含任何业务逻辑的理想中的“界面”。如果硬要说他什么有什么逻辑的话,那就是单纯的设置一下窗体的Caption或是让一个Button可用或不可用。View的所有的逻辑都抽离出来,放到了Presenter中,View只是一个转发事件和显示结果的咚咚,Presenter也变得Testable——引入MVP的终极目标。
   刚才的代码为了显示MVP本来的面目,没有引入“依赖注入机制”和“自动事件挂接机制”,既然要在CAB中用MVP的话,自然就要用到这两个很好的解耦利器,加入以后除了Persenter需要用到View的接口和Module接口外,没有其他的耦合关系。

   

 
 
public interface IView { // 一些事件的定义如果,包含所有你期望从View中Fire的事件 event EventHandler ViewLoad; // all method to update View void BindDataItem(); // just take as an example // properties string Caption // just take as an example { set ; } } public class View : UserControl,IView   { public event EventHandler ViewLoad; private View_Load( object sender, EventArgs e) { if (ViewLoad != null ) ViewLoad( this , new EventArgs); } public string Caption { set { this .Lable1.Text = value;} } // .... } public class Preseter { private IView view; private IModel model; public Preseter([ServiceDependency] IModel model,[CreadNew] Controller controller) { this .module = module; this .view = (IView);controller.WorkItem.Smartparts[ " SmartPartName " ];
         SubscriverToViewEvent(); }
// 订阅相应的Iview的事件 pirate void SubscriverToViewEvent() { view.ViewLoad += new EventHandler( this .ViewLoad); } public void ViewLoad( object sender, EventArgs e) { this .view.Caption = " HelloView " ; } // 处理View中触发的事件 void DealWithEvent(); // 更新View的方法,Module Fires 的事件 [EventSubscriber( " topic://ClinicDoctor/ClinicDoctorService/CaptionChanged " )] void UpdateView() { this .view.Caption = " HelloView " ; } } public interface IModel { [EventPublisher( " topic://ClinicDoctor/ClinicDoctorService/CaptionChanged " )] event EventHander UpdateCaptionEvent; // 调用Persenter,并要求它更新界面的一些方法 void CallPersenterToUpdateView(); } public class Model : IModel { public event EventHander UpdateCaptionEvent; // Fire Event void CallPersenterToUpdateView() { if (UpdateCaptionEvent != null ) UpdateCaptionEvent( this , new EventArgs()); } }

   
 
看到这里,是不是觉得就算是把现在的WinForm View 替换成 Web 窗体也不会有什么问题?灵活性变得非常高。

  总结一下:在结构上最大的变化就是,在MVP中Model不需要和View有任何联系,就算是View的接口也不用知道,Presenter控制了一切。当然Presenter中会有View的接口和Modle的接口,并且处理相应Modle和View Fire的事件,同时Update View或是操作Modle。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值