C# – MVP and presenter granularity

cmvpwinforms

We've been using the MVP pattern and Winforms with a fair amount of success. However, a question always pops-up about MVP:

What is a good granularity for presenters?

What I mean by that is: With Winforms, a fine-granularity usually works quite well for user controls. That way, it's easy to reuse user controls and use them as building blocks while designing more complex GUIs. However, having the same (fine-)granularity with presenters seems to be a problem.

On one hand, having coarse-grained presenters hinders the ability to use "plug-in" controls and it sorts of violate the DRY principle: Multiple presenters often need to implement the same logic (populate a list of customers, for instance), which is used by multiple, more complex, controls.

On the other hand, fine-grained presenters seem to limit the ability to reuse controls in different situations. For instance, an editing view might sometimes need to save the customer right away; sometimes it needs to link it to something else; sometimes is just needs to validate it; and so on. It often depends on the more complex control. But there's also a fair amount of shared behaviour.

Note that, in both cases, 1-presenter-1-view is achievable. What is considered "1-view" changes.

What is usually considered best-practices for presenter granularity using MVP and Winforms?

  • Fine-grained presenters and customizable behaviour through options or something of that nature?
  • Coarse-grained presenters and low presenter reusability?
  • Something else?

Disclaimer: We mainly use Supervising Controller but I think it also applies to Passive View. Sorry for the long question, too.

Best Answer

We use MVP at all of our clients and this is definitely a conversation that comes up in more than one occasion. How clean should our code behind classes and presenters be? Having said that, we have chosen to use the coarse-grained presenter approach. Basically, every form would have its own presenter and would only get and set properties of any of the controls on a particular form using its view. Populating controls-a call to a db to populate a combobox for example-is located in a public service class. Any validation of user inputted data is located in a BO class which can be reused by any and/or all of the presenters. I hope this helps.

Related Topic