Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between winforms in mvp pattern

I'm dealing with a big problem for developing my app. It's a Winforms base application on C# implementing the Model-View-Presenter pattern, but I am new to this method. I've searched everywhere and haven't found an answer to my issue.

I need to know how I can use this pattern to allow communication between winforms, and how the presenter must show them without coupling the presenter to the form. I've seen a way using Factory pattern, but don't understand how to implement it.

Any help or point in the right direction would be appreciated.

like image 491
Alexis Fernando Gonzlez Gonzle Avatar asked Feb 13 '13 16:02

Alexis Fernando Gonzlez Gonzle


2 Answers

Assertion

The Presenter is responsible for coordination between the View and Model (if following the Passive View implementation).

This could look like:

A View instantiating the Presenter and injecting itself into the Presenter:

IPresenter presenter;
public View() { presenter = new Presenter(this) }

A Presenter instantiating one or more views and injecting itself into the view(s):

IView1 view1;
public Presenter() { view1 = new View1(this) } 

IView1 view1;
IView2 view2;
public Presenter() { view1 = new View1(this); view2 = new View2(this); }

Example

In your case, a Presenter coordinating multiple views might look something like this (pseudo):

public class Presenter : IPresenter
{
  IView1 view1;
  IView2 view2;
  public Presenter() 
  {
    view1 = new View1(this);
    view2 = new View2(this);
  }

  private WireViewEvents()
  {
    view1.OnButtonClick += HandleButtonClickFromView1;
  }

  public void HandleButtonClickFromView1()
  {
    view2.SetSomeData();
    view2.Show();
}

In this example, an event raised by View1 is handled by the Presenter, data is set in View2, and View2 is shown.

Keep in mind that no matter what your implementation is, the goals of MVP are:

  • Separation of concerns (UI seperate from domain logic).
  • Increasing testability.

Keep that this is just a basic example of how a Presenter might coordinate multiple views. If you want to abstract your View creation from the presenter you might move the creation into another container that the Presenter calls in to to create Views and subscribe to their events.

like image 119
jeuton Avatar answered Sep 30 '22 07:09

jeuton


In MVP ,winforms should not communicate with each other. Form A knows its Presenter A, Form B knows its presenter B

Usually , you will modify model with form A through Prensenter A. Presenter B will listen to model changes,and will refresh Form B accordingly

If you need more coordination, you may consider using an Application Controller

See http://dotnetslackers.com/articles/designpatterns/The-Presenter-in-MVP-Implementations.aspx

like image 35
flamandier Avatar answered Sep 30 '22 07:09

flamandier