Let's say I have a form for representing a user.

When pressing the "Save" button, should a User object be:

  1. constructed by the View based on the input, then passed to the Model via the Presenter and then inserted into the database by the Model,
  2. constructed by the Model by using the View data (via the Presenter) and then inserted into the database by the Model.

Essentially, the question is whether or not the View should have access to Domain objects (such as User).

有帮助吗?

解决方案

The key principle in the MVP architecture (initially introduced by Talligent in 1996), is that the presenter acts as middleman between the view and the model.

The underlying idea is that in a client-server architecture, the presenter could be split between the the server where the domain model is maintained, and the client that takes care of the view.

This is why you should go for option 2. All data manipulation must go via the presenter.

Note that the original MVP allows a direct link between model and view, but only for reading purpose (via a distributed notification M->V to inform that data has changed, and a remote access V->M to read data, in a subject-observer relationship). Some claim however that channelling this via the presenter would be preferred.

许可以下: CC-BY-SA归因
scroll top