7
votes

I am trying to develop a Project Management System in my application following the Model View Presenter (MVP) pattern. My question is this, I have seen numerous examples of MVP but I haven't seen one with one Presenter and multiple Views. For example when a user opens a project the same project data can be viewed by a treeview, a datagrid and a chart. How do I cope with that?

4

4 Answers

4
votes

For making the code decoupled and easy to maintain I would suggest you to have one presenter for each View even if they look like very similar: so that each view will have its own presentation logic. If you got the same data that needs to be showed on more than one view you could share the View-model between the presenters but again I would suggest you to use different view model for each presenter(even if they are very similar)

2
votes

The way you would do it, is to abstract your view by placing it behind an interface and then wire up the presenter with the concrete implementation of your view.

That said, I'm not sure you would ever want to do it in the real world. The differences between dealing with a tree view compared to a chart would mean that you end up over generalizing things in your view interface and writing a lot of messy code in your view to fulfil the contract.

I would suggest keeping your presenter to view ratio at 1:1. If you want multiple views over the same data then it is your model that you should share across the presenters, so you are displaying the same data in different ways.

1
votes

You should almost always have one presenter instance per view instance.

Say you open a CustomerView, and its CustomerViewPresenter. That's one instance of each.

The you open another CustomerView, and another CustomerViewPresenter instance. That's two instances of each.

That doesn't mean that a given presenter always has to use the same view, in fact it should not. The presenter should talk to one view interface. You should be able to swap a real view for a mock view for testing.

0
votes

This question might be old but I just want to post my answer for future use.

If you have a view that could be used on multiple views, inheritance is the key.

Example:

using System;

// declaring an interface
public interface A {

    // declare a treeview;
    void treeView();
    // declare a datagrid;
    void datagrid();
    // declare a chart;
    void chart();
}

// The views of interface A 
// is inherited into interface B
public interface B : A {
    // declare a label or string
    string mystring  { get; set; }
}

C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.

public partial class Form1 : Form, B
{
    private Presenter presenter;

    public Form1()
    {
        InitializeComponent();
    }

    public string mystring
    {
        get
        {
            return textBox1.Text;
        }
        set
        {
            textBox1.Text = value;
        }
    }

    public void treeView()
    {
        presenter = new Presenter(this);
        presenter.GenerateTreeView();
    }

    public void datagrid()
    {
        presenter = new Presenter(this);
        presenter.GenerateDatagrid();
    }

    public void chart()
    {
        presenter = new Presenter(this);
        presenter.GenerateChart();
    }
}

And your presenter may look like this:

public class Presenter
{
    public B viewB;
    public DomainLogic model;

    public Presenter(B view)
    {
        viewB = view;
    }

    public void GenerateTreeView()
    {
        model = new DomainLogic();
        model.CreateTreeView();
    }

    public void GenerateDatagrid()
    {
        model = new DomainLogic();
        model.CreateDatagrid();
    }

    public void GenerateChart()
    {
        model = new DomainLogic();
        model.CreateChart();
    }

    public void GenerateString()
    {
        model = new DomainLogic();
        viewB.mystring = model.createString();
    }

}

In this case you may have 2 views in one presenter. Not literally 2 views. but inheritance can avoid duplication of codes to your Views.