The Nucleo MVP framework, at the presenter level, contains a complex system for cross-presenter messaging that each presenter can act on.  Using the presenter’s CurrentContext property, the EventRegistry object contains methods to subscribe and publish to events using a variety of syntaxes.

For instance, a presenter can subscribe to an event using the following syntax.  The event subscription can take place in the Initializing event of the view, or it can be in the constructor.  This example uses the former.

 

public ManyMessagesSamplePresenter(IManyMessagesSampleView view)
base(view)
{
view.Initializing += new EventHandler(View_Initializing);
}

void View_Initializing(object sender, EventArgs e)
{
this.CurrentContext.EventRegistry.Subscribe(
ListenerCriterion.EntityType<ManyMessagesPresenter>(),
(d) => 
{
HttpContext.Current.Response.Write(string.Format("A subpresenter published an event: {0}<br/>", id++)); 
});
}

 

The Subscribe method call takes two parameters.  The first parameter identifies the subscription, whereas the second parameter is the subscriber.  Let’s look at each individually.  There are a variety of ways to listen to events:

 

  • ListenerCriterion.Entity – Uses an object reference to filter events to.
  • ListenerCriterion.EntityType – Uses the type of some object to subscribe to events.
  • ListenerCriterion.Event – Uses an IEvent instance to attach to an event.  This will have more significance with the next release.
  • ListenerCriterion.Identifier – Uses a string (ideally a constant) to attach to as an event.

 

What this means is that there are many choices to use as the means to subscribe and publish events to.  An object can be the subscription key, a type, or a string identifier, is the primary purpose for this.  Next, we can supply one of two types of subscribers.  First, an action statement can listen to the event, and when the event fires, the action is called directly.  Secondly, an object implementing IEventReceiver can receive the event, if that is preferred.

Using this approach, when the event fires (is published), the PublishedEventDetails object contains the details about the event published.

 

public class ManyMessagesPresenter : BasePresenter<IManyMessagesView>
{
public ManyMessagesPresenter(IManyMessagesView view)
base(view) 
{
view.Loading += new EventHandler(View_Loading);
}

void View_Loading(object sender, EventArgs e)
{
this.CurrentContext.EventRegistry.Publish(
new PublishedEventDetails(ListenerCriterion.EntityType<ManyMessagesPresenter>()));
}
}

At any point in the process (here its when the view is loading, but it could be in response to any other event), the Publish method is responsible for the publishing of the event. The details of the event are also passed to the subscribe to the action or IEventReceiver instance.

In this way, messages can be passed through multiple presenters. Multiple presenters can subscribe to a single event, and vice versa.

You can get this framework at http://nucleo.codeplex.com.