Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP and multiple User Controls

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?

like image 882
SwDevMan81 Avatar asked Apr 24 '09 15:04

SwDevMan81


2 Answers

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

like image 137
Nikola Malovic Avatar answered Nov 15 '22 01:11

Nikola Malovic


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.

like image 40
Chap Avatar answered Nov 15 '22 00:11

Chap