What is a good way to implement events in a passive view?

I am studying a passive view template to keep a C # WinForms application easier to test and maintain.

So far, it has worked well, but I wonder if there is a better way to implement Events than how I am doing it now (and keeping them under control). This is how it looks (excluding code not related to this example). Basically, what I'm looking for, if there is a way to skip connecting events in both the presenter and in the form, I would prefer to do all the work in the presenter.

My view is as follows:

public interface IApplicationView { event EventHandler Find; } 

My lead is as follows:

 public class ApplicationPresenter { private IApplicationView _view; private IApplicationDomain _domain; public ApplicationPresenter(IApplicationView view) : this(view, new ApplicationDomain()) {} public ApplicationPresenter(IApplicationView view, IApplicationDomain domain) { _view = view; _domain = domain; HookupEventHandlersTo(view); } private void HookupEventHandlersTo(IApplicationView view) { view.Find += delegate { FindAction(); }; } public void FindAction() { // ... } } 

My WinForm looks like this:

 public partial class Form1 : Form, IApplicationView { private ApplicationPresenter _presenter; public event EventHandler Find = delegate {}; public Form1() { InitializeComponent(); _presenter = new ApplicationPresenter(this); HookupEvents(); } private void HookupEvents() { searchButton.Click += Find; } } 

Thanks!

+6
c # winforms passive-view
source share
2 answers

Another great resource for learning MVP with WinForms is Jeremy Millers Create your own CAB . I found it incredibly useful when I was studying,

The article on View to Presenter Communication will be useful to you; There is a good discussion here about using events against direct calls. Even better, Event Aggregator introduces a publish / subscribe mechanism that can be used instead of events, while keeping the code validated. This is the approach that I personally prefer, and was a good success.

+3
source share

Check out this passive view implementation example. It has a good way to connect / reject events between the view and the controller, which does most of the work in the controller.

0
source share

All Articles