简体   繁体   中英

MVP Framework for winforms

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.

Thanks!

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);
      }
   }
}

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.

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

This is not a framework, but I would read Jeremy Miller's Build Your Own Cab series before you settle on your design. He covers the various presentation patterns in WinForms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM