19

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!

ASh
  • 34,632
  • 9
  • 60
  • 82
fcartu
  • 355
  • 1
  • 9
  • 20
  • 1
    I think moving to WPF instead of working on old winforms would be a wise choiche. It's easy to implement the MVP pattern in wpf or silverlight. – Matteo Mosca Jul 29 '11 at 13:57
  • Have you thought about using the Smart Client Software Factory from Microsoft: http://msdn.microsoft.com/en-us/library/ff648753.aspx – Kane Jul 29 '11 at 13:58
  • possible duplicate of [Winforms - MVP examples](http://stackoverflow.com/questions/185993/winforms-mvp-examples) – stuartd Jul 29 '11 at 14:01
  • 1
    Well, i want to use WPF but the requeriments is for Winforms, the other problem is that i use VS 2005 that does not implement WPF. Smart Client implement a lot of stuff that dont apply to the project (Its not so complex) i was looking for something more practical. – fcartu Jul 29 '11 at 14:09

2 Answers2

40

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

Josh
  • 44,706
  • 7
  • 102
  • 124
  • What's the difference between `Presenter` and `Model`? – dance2die Jul 29 '11 at 15:32
  • Hi Josh, thanks for your help. Its better to implement it in that way that use a framework, given that is not a complex project.!! – fcartu Jul 29 '11 at 17:46
  • 3
    @Sung - The Presenter is responsible only for marshaling events and data back and forth between the Model and the View. In the example above there is no Model, but that is not to say you shouldn't have one. After you work with UI design patterns enough you will start to learn that they all basically do the same thing with subtle variations. MVC, MVP, and MVVM are very closely related. – Josh Jul 29 '11 at 18:07
  • Hi Josh, how can i represent datagridview or combobox inside view? – fcartu Aug 01 '11 at 14:11
  • @fcartu - A combobox is pretty easy as it is simply a list of some underlying entity. Any `IEnumerable` is suitable for binding. To get the currently selected item, you can either pass it via an event args, or simply create a seperate property on your view that represents the selection. Grids are a little different as they are heavily designed to work with the ASP.Net style of development... which is far from nice and testable. Probably worth a seperate question and answer as this comment is not long enough to explain :) – Josh Aug 01 '11 at 14:21
  • @Josh: Now I understand your code after reading up on MVC/MVP/MVVM. – dance2die Aug 02 '11 at 13:08
  • Josh, nice solution nut your generic base presenter class is implementing IView interface. But i think presenter should not implement view interface ? please correct me if i am wrong. – 0cool Feb 10 '12 at 09:17
  • 1
    @Neo - Ahh... I think what you are seeing is the generic Type Constraint. `where TView : class, IView` This is simply stating that when someone creates a concrete Presenter, it must have a property that implements IView. http://msdn.microsoft.com/en-us/library/d5x73970.aspx – Josh Feb 10 '12 at 17:40
4

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.

Eric Farr
  • 2,683
  • 21
  • 30