What is a good way to implement events in Passive View?
Asked Answered
A

2

7

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!

Avebury answered 5/11, 2009 at 17:49 Comment(4)
I can't help you but what you're asking about looks interesting. Could you point me to your favorite online resource that describes the technique you're trying to implement?Halifax
@Jay -- A quick Google search of "Model View Presenter" should give you all the information you need.Ozalid
@Jay Martin Fowler coined the Passive View design pattern when he retired the MVP pattern and split it up into two new ones. Where Passive View is one of them, see martinfowler.com/eaaDev/PassiveScreen.htmlAvebury
Thanks Robert, I'll check into it.Halifax
J
4

Another great resource for learning MVP with WinForms is Jeremy Millers Build Your Own CAB series. I found this incredibly useful when I was learning,

The article on View to Presenter Communication will be useful to you; there is a good discussion here on using events vs making direct calls. Even better, the Event Aggregator article presents a "publish/subscribe" mechanism that can be used instead of events, while keeping the code testable. This is the approach that I personally prefer, and have had good success with.

Jilli answered 6/11, 2009 at 8:44 Comment(1)
The Event Aggregator looks interesting, I will take a look at it this weekend and see how it turns out.Avebury
I
0

Check out this example of how to implement the Passive View. It has a good way of wiring/unwiring for events between the view and controller that puts most of the work in the controller.

Ingra answered 5/11, 2009 at 18:48 Comment(1)
Bad links. Both the old and newer one. Please do not post external links on SO. They tend to get decommissioned.Neglect

© 2022 - 2024 — McMap. All rights reserved.