Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 521
fcartu Avatar asked Jul 29 '11 13:07

fcartu


People also ask

What is MVP in WinForms?

Introduction. MVP is a user interface architectural pattern engineered that follows the separation of concerns in the presentation logic. It is derived from the MVC design pattern. In MVC, all the presentation logic is moved to the presenter, hence make it suitable for automated unit testing.

Does Microsoft still support WinForms?

"We continue to support and innovate in Windows Forms runtime," said Microsoft's Igor Velikorossov last month in announcing what's new for WinForms in . NET 6. He's a software engineer on the dev team for the 19-year-old product, a free and open-source graphical (GUI) class library included as a part of .

Is WinForms outdated?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

Is WinForms a framework?

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft . NET, . NET Framework or Mono Framework, providing a platform to write client applications for desktop, laptop, and tablet PCs.


2 Answers

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

like image 151
Josh Avatar answered Oct 25 '22 20:10

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.

like image 45
Eric Farr Avatar answered Oct 25 '22 20:10

Eric Farr