A typical MVP application might have a View method such as:
void setDisplayItems(List<DisplayItem> items);
Should the Presenter sort the list of items or is that a task for the view?
I feel this might be a task for the View, for two reasons:
-
If it were a task for the Presenter, then methods such as:
void addDisplayItem(DisplayItem item);
won’t work, as the Presenter can’t insert the item at the correct place.
-
The View can offer different sorting options, based on user selections.
However, I also can’t shake the feeling that this is too much “thinking” for the View to be doing. Can anyone comment on the correct choice for MVP?
I should note that I typically produce Swing-based Java applications (in case that allows for more specific answers).
1
The correct answer is (drum roll please) it depends. Are you using a UI framework that supports sorting natively? Are you sorting over a large sequence that would be best performed as a database operation? Does your UI framework support sorting through the database (e.g. IQueryable support in many .NET UI toolkits).
There are multiple approaches to MVP. You have the Passive View in which the Presenter has deep knowledge of the view via an Interface, there’s the supervising controller in which the presenter handles complex view logic and the rest is left to the view (so again if your view objects can handle sorting, let them do it if it’s not an expensive operation), finally is the Presentation Model which is pretty much equivalent to MVVM in which the Presenter exposes behavior as properties and the View reacts to those properties.
0
I would put logic like this in the Presenter. One of the important jobs of the Presenter is to put the data into the correct form for your View. This will make it easier for you to swap out views in the future.
In general, you should try to avoid putting a lot, if any, logic in your View. The only thing in your view should be visual display code and basic code for displaying data as prepared by the presenter.
2