MVP框架的WinForms [英] MVP Framework for winforms

查看:125
本文介绍了MVP框架的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个新的项目工作,我要实现MVP模式。目前对于使用这种模式的WinForms框架?我检查CAB,但我的项目并不复杂,以实现它,我寻找的东西更简单地实现和使用。

i'm working in a new project and i want to implement MVP pattern. There is a framework for winforms that use this pattern? I checked CAB but my project isn't complex to implement it, i search for something more simple to implement and use.

谢谢!

推荐答案

如果您正在寻找的东西简单... 那么你真的不需要一个框架。你可以推出自己的MVP模式。

If you are looking for something simple... then you really don't need a framework. You can roll your own MVP pattern.

编写的基类只需要几分钟的时间。

Writing the base classes takes only a few minutes.

//Base Presenter Class  
public class Presenter<TView> where TView : class, IView {
   public TView View { get; private set; }

   public Presenter(TView view) {
      if (view == null)
         throw new ArgumentNullException("view");

      View = view;
      View.Initialize += OnViewInitialize;
      View.Load += OnViewLoad;
   }

   protected virtual void OnViewInitialize(object sender, EventArgs e) { }

   protected virtual void OnViewLoad(object sender, EventArgs e) { }
}

//Base View  
public interface IView {
   event EventHandler Initialize;
   event EventHandler Load;
}

这是所有你需要开始。然后您可以定义以满足您的需求的新视图

That is all you need to get started. You can then define a new view to suit your needs.

public interface IPersonView : IView {
   String PersonName { get; set; }
   DateTime? DOB { get; set; }

   event EventHandler SavePerson;
}

创建使用视图的主持人。

Create a presenter that uses the view.

public class PersonPresenter : Presenter<IPersonView> {
   private IPersonDb PersonDB { get; set; }

   public PersonPresenter(IPersonView view, IPersonDb personDB)
      : base(view) {
      if (personDB == null)
         throw new ArgumentNullException("personDB");

      PersonDB = personDB;
   }

   protected override void OnViewInitialize(object sender, EventArgs e) {
      base.OnViewInitialize(sender, e);

      View.PersonName = "Enter Name";
      View.DOB = null;

      View.SavePerson += View_SavePerson;
   }

   void View_SavePerson(object sender, EventArgs e) {
      PersonDB.SavePerson(View.PersonName, View.DOB);
   }
}

和最后投入使用的一种新的形式。

And finally put it into use in a new form.

public partial class Form1 : Form, IPersonView {
   private PersonPresenter Presenter { get; set; }

   public Form1() {
      Presenter = new PersonPresenter(this, new PersonDb());

      InitializeComponent();

      InvokeInitialize(new EventArgs());
   }

   public string PersonName {
      get { return tbName.Text; }
      set { tbName.Text = value; }
   }

   public DateTime? DOB {
      get {
         return String.IsNullOrWhiteSpace(tbDOB.Text) ?
                  (DateTime?) null :
                  DateTime.Parse(tbDOB.Text);
      }
      set {
         tbDOB.Text = String.Format("{0}", value);
      }
   }

   public event EventHandler Initialize;

   public void InvokeInitialize(EventArgs e) {
      EventHandler handler = Initialize;
      if (handler != null) {
         handler(this, e);
      }
   }

   public event EventHandler SavePerson;

   public void InvokeSavePerson(EventArgs e) {
      EventHandler handler = SavePerson;
      if (handler != null) {
         handler(this, e);
      }
   }
}



我喜欢杰里米·米勒的东东许多。我已经使用智能客户端软件工厂......但这些都是关于解决非常大的复杂的问题。有混的,因为它掩盖了MVP模式,开始与简单许多其他的模式。

I like Jeremy Miller's stuff a lot. And I have used the Smart Client Software Factory... but those are about solving very large complicated problems. There are so many other patterns mixed in that it overshadows the simplicity of the MVP pattern to begin with.

从简单的开始,当你开始运行到粗点,那么你可以开始在喜欢的东西服务定位器和事件聚合器添加。

Start simple and as you start to run into rough spots, then you can begin to add in things like Service Locators and Event Aggregators.

MVP模式真的是很微不足道的实施。我希望这可以帮助把你从一个运行更加快速启动。

The MVP pattern is really very trivial to implement. I hope this can help to get you off to a running start more quickly.

干杯,结果
乔什 -

Cheers,
Josh

这篇关于MVP框架的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆