Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP: Is it the View or the Presenter that should know of the Model?

Relatively new to patterns, let me straight away show an example in the context of WinForms.

I have a basic MVP Passive View structure, which one should I go ahead with:

public partial class UserView : Form, IUserView
{
    public event EventHandler Save;

    public UserView()
    {
        InitializeComponent();

        new UserPresenter(new UserModel(), this);
    }
}

public class UserPresenter
{
    public UserPresenter(IUser model, IUserView view)
    {
        view.Save += (sender, e) => model.Save();
    }
}

or

public partial class UserView : Form, IUserView
{
    public event EventHandler Save;

    public UserView()
    {
        InitializeComponent();

        new UserPresenter(this);
    }
}

public class UserPresenter
{
    public UserPresenter(IUserView view)
    {
        var model = new UserModel();
        //assuming I have the logic to bind property values from View to Model
        view.Save += (sender, e) => model.Save();
    }
}

My questions are:

1) Who should know of the concrete instance of model User, View or Presenter?

2) What will be the benefit in that case?

3) Suppose my Model is never dependent on the View. In that case what's wrong if View knows Model? After all UserView is made to present UserModel isn't it?

4) If Presenter should interact with only interfaces of Model and View, then to call model.Save in Save eventhandler, where do I get the concrete instance of Model from?

There are two duplicate questions here and here, but they aren't exactly dealing with my scenario I guess..

like image 745
nawfal Avatar asked Jan 17 '13 19:01

nawfal


People also ask

What does the presenter do in MVP?

The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.

How does MVP differ from the MVC model view controller pattern?

MVP pattern overcomes the challenges of MVC and provides an easy way to structure the project codes. The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase.

What is the MVP design pattern?

Model View Presenter (MVP) The MVP pattern is similar to the MVC pattern. It is derived from MVC pattern, wherein the controller is replaced by the presenter. This pattern divides an application into three major aspects: Model, View, and Presenter.

What is presenter in MVP Android?

Example of MVP Architecture The role of the Presenter class is to keep the business logic of the application away from the activity. Below is the complete step-by-step implementation of this android application. Note that we are going to implement the project using both Java and Kotlin language.


3 Answers

What's wrong if view knows model? After all UserView is made specifically for UserModel isnt it?

Nothing. It's accepted practice in the Supervising Controller variant of the MVP pattern. The view interacts directly with the model for simple operations while more complex operations are marshalled throught the presenter. While in Passive View, everything goes through the presenter.

Additionally, see Jeremy Miller's Build your own CAB series to get a better idea on the differences between the two approaches: Supervising Controller and Passive View.

like image 30
Big Daddy Avatar answered Oct 20 '22 01:10

Big Daddy


The Presenter should know about the Model, the View should not. A presententation layer is a good idea in many user interface applications. A presentation layer is simply an adapter. It presents an interface that's easy for a user interface layer to use (i.e., it presents lots of events, bindable properties, and so on) while obscuring the underlying data layer. This makes the data layer easier to re-use.

EDIT

So why can't the view just talk to the model directly? It certainly can. The problem is that there is usually an impedence mismatch between the model and the view. In other words, the programming interface that's natural for the view to use does not match the interface that's natural for the model to expose. If you adapt the model to suit the view's needs, then you end up creating a strong coupling between the model and the particular type of interface you're using.

For example, your app might be a GUI app today, but what if tomorrow you're asked to produce a version for the cloud? The events and bindable properties that are helpful for Winforms will just get in the way when you try to switch to WCF Rest. If you use a presentation layer, then adapting your code to the new environment will be much easier.

like image 32
Peter Ruderman Avatar answered Oct 19 '22 23:10

Peter Ruderman


Strictly speaking, you should have the following rules:

  1. Model does not know the View or the Presenter.
  2. View does not know the Model or the Presenter.
  3. Presenter knows both Models and Views, but only through their interfaces.

The Presenter coordinates all communication between the Model and the View, typically by handling events that are raised by the View. So to answer your questions:

1) Who should know of the concrete instance of model User, View or Presenter?

Ideally, neither. The Presenter should be communicating with UserModel through an IUserModel interface. The concrete instance is injected into the Presenter (e.g. through its constructor).

2) What will be the benefit in that case?

The primary benefit is for automated unit testing. You can inject mock Models or Views to test units in isolation.

3) Suppose my Model is never dependent on the View. In that case what's wrong if View knows Model? After all UserView is made to present UserModel isn't it?

There's nothing inherently wrong with it. There are variations of MVP that support direct communication from the View to the Model, typically to take advantage of data binding. You lose some testability in exchange for not having to write the binding code from scratch.

4) If Presenter should interact with only interfaces of Model and View, then to call model.Save in Save eventhandler, where do I get the concrete instance of Model from?

Depedency injection, such as the simplified example shown below.

public class SamplePresenter
{
     public SamplePresenter(ISampleModel model, ISampleView view)
     {
          view.Saved += (sender, e) => model.Save();
     }
}

public interface ISampleModel
{
     void Save();
}

public interface ISampleView
{
     void Show();
     event EventHandler Saved;
}

public class Program
{
     [STAThread]
     static void Main()
     {
          ISampleModel model = new SampleModel();
          ISampleView view = new SampleView();
          SamplePresenter presenter = new SamplePresenter(model, view);
          view.Show();
     }
}
like image 72
Bob Avatar answered Oct 19 '22 23:10

Bob