Tag: eventhandler

Event wire up in MVP

I was struggling in MVP eventhandler for a long time. What is the correct way to link view events with presenter method. Before I used to simply put the presenter method code directly into my UI control’s event, as the example shown in Billy McCafferty,

protected void btnAddDays_OnClick(object sender, EventArgs e)
presenter.AddDays(txtNumberOfDays.Text, Page.IsValid);

This is OK but I think this needs UI developer know more about presenter’s behaviors, what if  presenter has many methods? Alex Mueller gave out a nice solution by introducing EventHandler in view, which I like it better. In fact, Billy is doing the similar thing but it’s in another way, eventHandler in presenter to enable view hooking up to, this causes the view coding a little bit heavier. Don’t we always want to keep view thinner?

protected void Page_Load(object sender, EventArgs e)
EditProjectPresenter presenter =
new EditProjectPresenter(editProjectView);
// I think this code should move into presenter’s constructor. Like Alex Mueller did.
presenter.ProjectUpdated += new EventHandler(HandleProjectUpdated);

private void HandleProjectUpdated(object sender, EventArgs e)
MyPageMethods.ShowProjectSummary.Show(projectId, userId));

I also like Alex’s name covention for eventhandler in MVP.

View         Presenter

RequestAction     ActionListener

When I played his code in VS2008, those null check and extra declaration seem not necessary any more. Instead of doing this:

protected void btnEdit_Click(object sender, EventArgs e)
// Raise our event
public event EventHandler RequestCustomerDetails;
public virtual void OnRequestCustomerEdit()
EventHandler<SingleValueEventArgs<string>> eventHandler = RequestCustomerEdit;
if (eventHandler != null)
eventHandler(this, null);

I can now make it much simplier:

btnEdit_Click +=    RequestCustomerDetails;

Not sure is it correct yet. I will update when I found new problem for this.