I recently found out about Presenter First and read their whitepapers and blogs, etc.
In most of the examples I found, the events are not declared directly on the interface but rather as a method for it. For example,
public interface IPuzzleView
{
void SubscribeMoveRequest(PointDelegate listener);
// vs
event PointDelegate MoveRequest;
}
I don't understand exactly why. I thought I saw a paper/article/blog somewhere that explains the reasoning behind this but I can no longer find it. The said text also contained snippets of unit testing code as well - I know this because I remember saying to myself, that one of the unit test was incorrect.
UPDATE:
The following is an example for comparison:
public class Collect
{
public static CollectAction<T> Argument<T>(int index,
CollectAction<T>.Collect collectDelegate)
{
CollectAction<T> collect = new CollectAction<T>(index, collectDelegate);
return collect;
}
}
public interface IApplicationView
{
event EventHandler Load;
// or
void SubscribeLoad(Action action);
}
Mockery mockery = new Mockery();
IApplicationView view = mockery.NewMock<IApplicationView>();
IApplicationModel model = mockery.NewMock<IApplicationModel>();
Subscribe style:
Action savedAction = null;
Expect.Once.On(view).Method("SubscribeLoad").Will(
Collect.Argument<Action>(0,
delegate(Action action) { savedAction = action; }));
Expect.Once.On(model).Method("LoadModules");
new ApplicationPresenter(view, model);
savedAction();
mockery.VerifyAllExpectationsHaveBeenMet();
vs. Event:
Expect.Once.On(view).EventAdd("Load", Is.Anything);
Expect.Once.On(model).Method("LoadModules");
new ApplicationPresenter(view, model);
Fire.Event("Load").On(view);
mockery.VerifyAllExpectationsHaveBeenMet();
FYI, the event style above will not work as is since ApplicationPresenter gets garbage-collected right away and the wiring never happens.