Home  /  Questions  /  Question



555   99.9
Jun 21, 2010


Using an MVx approach for implementing User Controls

Until recently, I thought about MVx patterns as technological approaches to seperate a domain model from the view layer, given that everything you need for let's say presenting the data does already exist (i.e. a TextBox, a Label, a ComboBox, etc.).

However, imagine for a while you need to create your own user controls, because you need a slightly more complex behaviour on existing controls. Given that you choose an MVx pattern to design your application, the question arises of how to implement the user control for as it also encapsulates a View, some sort of Presenter/Controller and a Model.

Example:
Your design document (you've got one, don't you?) says you need to create some sort of form that allows the user to enter a detailed description of some business case. However, it also says: The standardized desktop only has a resolution of 800x600 and by the way, did we mention that there are like two dozen other controls needed to be placed on the very same form? Thank you.

So you think of a textbox naturally,... but need a way for the user to comfortably enter way more than just one or two lines. You place a little icon right of the textbox with a neat zoom picture on it and every time the user clicks on it, he or she gets a modal popup dialog with a textbox the size of a whole planet (something below 800x600 pixels, that is) that only wants you to enter the description of a simple business case. Since you anticipate that this kind of input method might be used even in other places, you decide to create a usercontrol. You even add some properties so that the form designer can choose whether the zoom icon is placed left or right of the textbox, top bottom or even in the middle (what ever that might be good for).

So what do we have here now, I ask you.

  • A view represented by a textbox and a zoom icon (picturebox)
  • A model representing some settings and the text itself maybe?
  • A Controller/Presenter that alters the view and controls its behaviour based on the model

Except for the view which' controls are most certainly NOT the same between different UI frameworks (WinForm, WPF, SilverLight, etc.), the logic and the model behind will most certainly ARE the same.

So would it make sense to seperate the stuff from eachother and if so, how? Or are we already satisfied with the text being stored in the actual domain model and therefor already IS part of our overall MVx design? How do we get Custom/User Controls more or less "portable" withouth having to implement an MVx pattern within the V of an MVx pattern? Or am I even talking nonsense?

Would love to hear about your opinions and experiences.

-Chris.




1,128   99.9
Jun 28, 2010
Hi Christian,

I would implement the user control using MV*, for a couple of reasons...

* Separations of concerns; mixing UI / Logic / Data usually leads to
something that is harder to maintain in the long run.

* Testing; using any of the MV* patterns usually makes it easier to
test the control.

* Consistency; If you develop your control using a well known pattern
makes it easier for other to get a grip on it. As well as yourself, six
months later :)

Of course, there is always a trade off. If the user control is not very
complex it might not be worth the effort.

Keep in mind that following a strict MVP style where the view doesn't
have any relation to its presenter makes it impossible to create a
user control to use in Visual Studio's designer. In this case MVVM
would be better off (if you're doing WPF). You may of course modify
the MVP pattern so that the view creates the presenter and that would
solve the problem.

Good Luck!