MVP and multiple User Controls

我的未来我决定 提交于 2019-11-29 21:04:11

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.

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

Charles Bretana

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.

Old question, but I'm going to pipe up and have to disagree with the other answers: you don't want one presenter per UserControl, any more than you want a unit test on every UserControl - that would be a blind misuse of a design pattern.

A UserControl is not a View.

Each logical area of your application should have one presenter; how each is broken up - how many controls, what shows what - is solely a concern of composition.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!