Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

  1. 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.

  2. 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).

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you, some helpful links there. –  Duncan Mar 31 '13 at 10:55
add comment

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.

share|improve this answer
    
Thanks for the response. How would you approach the insertion of data into a list? –  Duncan Mar 26 '13 at 15:53
    
I'm not sure of the details, but I would build the list in a presenter method, and the view could call that method in the presenter. The basic form of communication between the two would be a sorted list. –  Oleksi Mar 26 '13 at 16:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.