I am learning the Passive View pattern to keep my C# WinForms application easier to test and maintain.
It has worked well so far but I wonder if there is a better way to implement Events than the way I am doing it now (and keeping them testable). This is what it looks like (omitting code not relevant for this example). Basically what I am looking for if is there is a way to skip hooking up events both in the presenter and in the form, I would prefer doing all work in the presenter.
My view looks like this:
public interface IApplicationView
{
event EventHandler Find;
}
My presenter looks like this:
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:
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!