tags:

views:

270

answers:

1

Hello

I have written a MVP project where the View is a WinForm that implements my IView interface. I am in the process of reviewing the code, improing it where I can and would like to ask your thoughts regarding how the view and presenter interact. Which of the following is bext practisce in your opinion?

1) Expose methods of the presenter class for the view to use. (i.e make them public). 2) Have the presenter listen to events raised by the View class.

For example my MVP uses a service that comunicates via a serialport. To connect to the remote device my view calls the public Presenter method Connect() which then calls the appropriate service methods.

Would it be better practise to raise a Connect() event and have the presenter listen for it?

Thanks in advance.

+3  A: 

Generally I work so that the view is dependant on the presenter and the presenter is dependant on the model. This means the same model can be used by multiple presenters and the same presenter can be used my multiple views (different UI layouts, or Winforms vs Web etc). To facilitate this the view calls public methods on the presenter and listens to events from the presenter to say when data has changed.

For an example of why you may want to work this way imagine an interface that has two views one for a beginner user and one for an expert, with the beginner view showing a subset of the expert commands. If the same presenter is used for both of these views then the beginner view will need to expose events for all the expert commands since the presenter needs them to be there so it can bind to them - even though they will never be triggered. On the other hand the presenter can expose methods for all of the expert options and the beginner view just never calls them, this means that neither the view nor the presenter are implementing unused functionality.

Martin Harris
Agree! We changed to option 1 (having the view call into the presenter) after using option 2 for a long time in a project. Less complexity and less work.
Arjan Einbu