tags:

views:

446

answers:

3

I'm using the MVP pattern in a windows form app. I need to change a radio button on the view. I can do this by exposing a Boolean property on the view, but should I be using events to manipulate the view instead?

+1  A: 

Using "plain language" analysis, I would say that "whether or not the radio button is displayed is a property of the view" and therefore use an actual property to communicate this to the View.

The other (technical) possibility would be to have a event on the Presenter, i.e. RadioButtonVisibilityChanged, which is listened to by the View and passes the new visibility through the EventArgs. This runs contrary to the View being independent and ignorant of the Presenter and therefore undermines the MVP pattern. I advise against such nonsense.

David Schmitt
Is there any situation where you think an event would be more fitting?
+5  A: 

It's a matter of purity vs being pragmatic... and a bit of personal style. Shouldn't matter... events are just more work than normal methods but more decoupled. Personally

  • I like to keep views decoupled or unaware of the presenters, hence Views communicate to the presenter by raising events. This eliminates the need for the view to have a reference to the presenter. Keep Views thin and dumb.
  • The presenter on the other hand has a member reference to the view (and the model) usually. Hence it can talk to the view by making method calls via an interface (permits views to be substituted as long as they conform to the IView interface). e.g. In your case, Set_X_Option(eOptionEnum) would be a member of the IView Interface, which the presenter can then invoke appropriately.

However you could remove this IView dependency (presenter has reference to an IView, that needs to be instantiated and plugged in) as well by using events both ways... however I find that its too much work. The above scheme has never failed me... yet.

Gishu
so the presenter is still created I'm the view constructor, but there are no more references to it. Is that what you mean?
No. The presenter creates the view (or you could use Dependency Inversion to eliminate this) and wires up the event handlers in the presenter to the IView events. The View implementation just has to implement all the IView methods and raise events appropriately... absolute unaware of the presenter.
Gishu
http://tech.groups.yahoo.com/group/TestFirstUserInterfaces/files/ TDD_GUI_DotNet-Gishu_Src.zip
Gishu
+1  A: 

Typically the controller changes the view through properties and subscribes to events raised by the view to know about changes. Here's a nice example of MVP, even though in Asp.NET works basically the same for Windows Forms.

mmiika