OT: How to re-trigger or bubble an event

OT: How to re-trigger or bubble an event

Old forum URL: forums.lhotka.net/forums/t/6586.aspx


Jack posted on Monday, March 09, 2009

I have a Views/Presenters/Controller type implementation for one use case that I basically need to respond to a slew of button clicks such that I wanted to consolidate all the logic in a single place.

Is there a shorthand notation to declare an eventhandler that basically will retrigger the event?

I'm trying to do:

Controller += Presenter.Event
Presenter += View.Event

View.Click.Invoke Event

Presenter --> Controller.DoProcessing

Thus the presenter can do something if it wants but most cases does nothing but bubble it up.

I've ended up adding another method in the Presenter to pass it along.

void  View_MyEpmActionRequested(object sender, DataEventArgs<MyEpmViewActionData> e)
        {
            MyEpmActionRequested(sender, e);
        }
I thought there was a shortcut to avoid this method.  I'm planning down the road to use some commanding but that is for another day.

Thanks

jack

SonOfPirate replied on Tuesday, March 10, 2009

So, if I understand you correctly, your Presenter is handling events from the View and you want to bubble them to the Controller?

It looks like you have the right idea.  I'm really not sure of any other way to do it other than adding an event handler in your presenter, wiring it up as you are and triggering the event that you've defined in your presenter from the handler.  For example:

public class View
{
    public event EventHandler ButtonClicked;
}
 
public class Presenter
{
    public Presenter(View view)
    {
        view.ButtonClicked += new EventHandler(View_ButtonClicked);
    }

    private void View_ButtonClicked(object sender, EventArgs e)
    {
        OnButtonClicked(e);
    }
 
    public event EventHandler ButtonClicked;
 
    protected virtual void OnButtonClicked(EventArgs e)
    {
        if (ButtonClicked != null)
            ButtonClicked(this, e);
    }
}
 
public class Controller
{
    public Controller(Presenter presenter)
    {
        presenter.ButtonClicked += new EventHandler(Presenter_ButtonClicked);
    }
 
    private void Presenter_ButtonClicked(object sender, EventArgs e)
    {
        // Do something
    }
}

Hope that helps.

 

Copyright (c) Marimer LLC