views:

825

answers:

3

I’m trying to use the MVP pattern and I’m running into a design problem. I’m developing an application that will have several UserControls. The UserControls themselves have nothing to do with one another and only represent a subset of the actual model. From what I’ve read, people tend to say you should use one Presenter per View. This seems to make sense, but if I have 30 UserControls, do I really want 30 Presenters? On the flip side, if I have 1 Presenter and 1 View that represent the entire “application” view, then I’ll have bloated View and Presenter interfaces. Then each View would have to implement methods that have nothing to do with it. My question is, is there a better way to handle multiple UserControls, or should I just create 1 Presenter for each View?

A: 

Each View doesn't have to implement the same interface... Why not define the interfaces for each control, and have one Presenter for the entire screen that contains all the controls? The Presenter can "wire up" the events on each view according to what interface defined events each view requires, to appropriate event handlers on the Presenter (and on a controller if you are doing MVPC). You may also need another interface to represent the Presenter functionality that ALL view need access to in common...

  • If you're doing MVPC then view events that affect the Model wopuld be "handled" in the Controller, whereas View events that only affect other parts of the View would be handled on the Presenter.
Charles Bretana
+9  A: 

It would make more sense to group the code that is related in one object. So, in this case, if the views are specific groupings of related code, then the presenter would also mimic these groupings. To have a "global" presenter for different views would group unrelated code in one object. It would definitely bloat the interface for the presenter as well. Check out the single responsibility principle.

Now, you could have one Presenter Manager class perhaps that could give you access to each presenter interface, like the Interface Segregation Principle states, by either inheritance (have a global concrete presenter that implements many presenter interfaces..which kind of violates the single responsibilty) or aggregation (having individual presenters for each interface and get functions...thus the global interface would the the get functions) or a combination of both (global presenter being somewhat of an adapter).

I think the best solution though would just to have 30 different presenters.

Chap
The danger here, is that multiple views could be loading the same data entity independently. Or have I missed the point?
Junto
If you have shared data that needs to be used by multiple controls, there should be a separate layer that serves that data to them. Easiest is probably some datafetcher object (or objects) that is passed into each control's presenter on creation from which they can request the data they need when they need it.
McMuttons
+6  A: 

You should be doing one presenter per one control because of:

  • that would allow you to have focused unit tests tackling only that control
  • increased maintainability due to the fact you won’t need to support gigantic presenter containing union of presentation logic of all controls
  • that would prevent redundancy in cases of having same control on multiple pages
  • Increases SRP by having controls focusing on their specific logic while the page performs container specific roles:

There are two problems usually mentioned related to the decision “presenter per control”:

  • Shared context is problem where due to the fact that all of the controls are just showing different pieces of the same page data context, that situation may look like a problematic use case leading to a lot of data retrieval redundant code in every of the controls. That is easily solvable through dependency injection where page (or controller) performs single data retrieval and then inject the data context object into every of the presenters\views (usually implementing some interface enabling that). In case of MV-VM pattern (Silverlight, WPF) same effect can be achieved through data bounding where the page would set its DataContext which would then be used from views itself
  • Communication between views on the same page is second problem which is easily solvable using couple of approaches:
  • Controls are publishing events to which page subscribes and then makes direct calls to appropriate methods in other controls (page is container to all controls meaning it is aware of all of their members)
  • Observer design pattern
  • Event aggregator pattern

In each one of this approaches controls are communicating with each other without being aware of each other

Nikola Malovic