Archive for February, 2009

Presenter First: Simple Presenters

Tuesday, February 17th, 2009

While reading this article, please keep in mind that I am a novice in using Presenter First.  The following technique has not been fully tested in a production environment, and may have drawbacks that I have not anticipated.


In developing an obscenely large and complicated personal project, I have become a big fan of the Presenter First design pattern.  It ‘clicks’ with me in a way that standard Model-View-Controller and Model-View-Presenter patterns simply have not.

A quick explanation of what makes Presenter First a little different: A Presenter contains a reference to a Model interface and a View interface.  The Presenter subscribes to events on the Model and View.  The Model and View can only communicate with the Presenter by raising these events.  The Model and View do not have any direct reference to each other.  This allows you to test the Model and Presenter logic without creating a View.

I highly recommend reading the Presenter First white paper (PDF) for more details.

In my project, I’ve been trying to follow a test-driven development approach, where I write test cases (using NUnit and NMock2) before I write the actual code.

I immediately ran into issues testing event subscription and event firing.  It was difficult to test the presenter, because it required the test to fire an event owned by the mock Model.

For example, this was my presenter code for a simple History presenter. The goal is the History model fires the MessageLogged event when a new message has been added to the History log, which will prompt the Presenter to call the View’s UpdateHistory function.

public interface IHistoryModel
{
    event Action MessageLogged;
    string Message { get; }
}

public interface IHistoryView
{
    void UpdateHistory( string aMessage );
}

public class HistoryPresenter
{
    private readonly IHistoryModel mModel;
    private readonly IHistoryView mView;

    public HistoryPresenter( IHistoryModel aModel,
                             IHistoryView aView )
    {
        mModel = aModel;
        mView = aView;

        aModel.MessageLogged += OnMessageLogged;
    }

    private void OnMessageLogged()
    {
        mView.UpdateHistory( mModel.Message );
    }
}

To test this, I mocked up a Model and View with NMock2, but NMock2 does not have any direct provisions for firing events that are on mock objects. I googled around, found a way to create MockEvents, and I found myself with a terrible, complicated test case.

To be fair, the Presenter First white paper does recommend that events should not be specified in the interface itself; they should be hidden by a separate function, like ‘SubscribeMessageLogged’. I found this concept ridiculous, but I started to understand why.

After struggling with MockEvents for several days, I had a sudden epiphany that would greatly simplify everything. I changed the Model event to provide the associated data as a parameter (a behavior that was also discouraged by the white paper) and transform the Presenter into a brain-dead connector.

public interface IHistoryModel
{
    event Action<string> MessageLogged;
}

public interface IHistoryView
{
    void UpdateHistory( string aMessage );
}

public class HistoryPresenter
{
    public HistoryPresenter( IHistoryModel aModel,
                             IHistoryView aView )
    {
        aModel.MessageLogged += aView.UpdateHistory;
    }
}

And… that’s it. The Presenter doesn’t even need any additional methods outside of the constructor!

The test case is super simple as well:

[TestFixture]
public class HistoryPresenterTest
{
    [Test]
    public void Constructor()
    {
        var lMockery = new Mockery();

        var lView = lMockery.NewMock<IHistoryView>();
        var lModel = lMockery.NewMock<IHistoryModel>();

        Expect.Once.On( lModel ).EventAdd(
            "MessageLogged",
            new Action(
                lView.UpdateHistory ) );

        new HistoryPresenter( lModel, lView );

        lMockery.VerifyAllExpectationsHaveBeenMet();
    }
}

The mock Model expects that it will be connected to the View’s UpdateHistory method, and that’s all of the testing that is required. There is no need to raise the MessageLogged event, because that would simply test the functionality of C# events.

The advantage of this simple presenter is even more profound as you add new events to the Model and View. Now, all that is needed is one event, one method, one line connecting the model and view in the Presenter, and one line in the test case.

As long as each event can be tied to a corresponding method, this makes writing presenters (and their tests) extremely simple.

Simple Presenter Example Source Code (26 KB, requires Visual Studio 2008 and Nunit 2.4.8)

Review: Gran Torino (Movie)

Monday, February 9th, 2009

Comedy of the year, until the third act.

Helen: The Blind Chicken

Sunday, February 8th, 2009

Say hello to Helen.

Helen Introduction

She is perched outside of the chicken pen.  She is blind.


Last weekend, the Michigan weather had warmed up considerably (considerably for Michigan), and it was above freezing.

As the sun rose in the sky, my father opened the large door to the chicken pen.  The chickens gradually wandered out, and mingled about outside for the afternoon.  They enjoy wandering about the yard.  Surprisingly, the cats don’t bother them at all.

As afternoon darkened to evening, my father herded them back into the pen.  Alas, one of the chicken’s poked its head outside for another second, and was subsequently rewarded by having its head squished quite thoroughly as my father swung the large door shut.

For reasons that remain unclear, my mother took it upon herself to care for this chicken.  She set up a small box in the basement for it to stay in, and fed it milk.  As the days went by, she grew more attached to it, and even gave it a name: Helen.

Helen Being Pet

Alas, Helen cannot see, and is a rather daft.  She responds to sounds and touch, but primarily spends her time standing in one spot, turning around in circles.  She will not eat for herself, and my mother must feed her milk.

Helen Closeup

As you can see, Helen isn’t looking so well.  One of her eyes is missing, and one is very cloudy.  Her crown is blackened and flaccid.