Export (0) Print
Expand All

Walkthrough: The Unity Event Broker Extension QuickStart

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The Event Broker Extension QuickStart demonstrates how you can extend the Unity container by adding a custom extension. The QuickStart implements an event broker for the container as a container extension and demonstrates the new extension using the StopLight application discussed in Walkthrough: The Unity StopLight QuickStart.

The Event Broker Extension QuickStart contains three projects:

  • EventBroker. This project implements a simple publish and subscribe mechanism that supports multiple event publishers and multiple subscribers.
  • EventBrokerExtension. This project implements the custom container extension that allows applications to publish and subscribe to events using attributes or explicitly using code.
  • StopLight. This project is basically the same as that described in the Unity StopLight QuickStart, but it uses the custom container extension to manage the publishing of, and subscription to, two events within the application.

For information about how you can create and use custom container extensions, see Creating Container Extensions and Using Container Extensions.

Figure 1 shows the classes and architecture of the Event Broker Extension QuickStart.

Ff650677.c3f35fea-19dd-4f7a-831b-ccbce2e18bc9(en-us,PandP.10).png

Figure 1
The architecture, classes, and structure of the EventBroker Extension QuickStart

If you compare Figure 1 to the structure of the StopLight QuickStart shown in Walkthrough: The Unity StopLight QuickStart, you can see that the EventBroker Extension QuickStart has the following additional features:

  • The Program class, which registers the type mappings in the container and calls the Resolve method to instantiate the main StopLight form, also adds the SimpleEventBrokerExtension to the container.
  • The SimpleEventBrokerExtension, which inherits from the UnityContainerExtension base class, creates an instance of the EventBroker class that implements the publish and subscribe pattern for distributed events.
  • The EventBroker class creates an instance of the PublishedEvent class that provides the facilities for maintaining a list of event subscriptions and raising events to registered subscribers.
  • The StopLightPresenter, StopLightSchedule, and RealTimeTimer classes include attributes that register event publications and subscriptions with the SimpleEventBrokerExtension class.

The Event Broker Extension QuickStart demonstrates the following features of the Unity Application Block and the custom container extension mechanism:

A Unity container extension is a custom class that inherits from the UnityContainerExtension base class and implements extra functionality beyond that available from the container. The extension can access the container and receives notification of registrations taking place within the container. This section describes the significant features of the EventBrokerExtension project.

The class SimpleEventBrokerExtension inherits UnityContainerExtension and creates a new instance of the EventBroker class from the SimpleEventBroker project. It then overrides the Initialize method of the UnityContainerExtension base class.

The code in the Initialize method of the SimpleEventBrokerExtension class, which follows, adds the new EventBroker it creates to the container's Locator so that other classes can locate and reference it easily, and then it adds two strategies to the Unity build pipeline. It adds a reflection strategy to the PreCreation stage that will detect the two attributes that the extension uses (the PublishesAttribute and the SubscribesToAttribute) and a wire-up strategy to the Initialization stage that registers publishers and subscriber delegates with the EventBroker. Finally, it exposes the new EventBroker it created as a property.

public class SimpleEventBrokerExtension : UnityContainerExtension,
                                          ISimpleEventBrokerConfiguration
{
  private EventBroker broker = new EventBroker();

  protected override void Initialize()
  {
    Context.Locator.Add(typeof(EventBroker), broker);
    Context.Strategies.AddNew<EventBrokerReflectionStrategy>(UnityBuildStage.PreCreation);
    Context.Strategies.AddNew<EventBrokerWireupStrategy>(UnityBuildStage.Initialization);
  }

  public EventBroker Broker
  {
    get { return broker; }
  }
}

The EventBroker class holds a Dictionary containing entries that map event names to publishers of that event and exposes methods to register publishers and subscribers (named RegisterPublisher and RegisterSubscriber). It also exposes methods to unregister publishers and subscribers and to get a list of publishers or subscribers for a specified event name. Much of the functionality for these methods is in the PublishedEvent class, which stores and exposes lists of all publishers and subscribers.

The wire-up strategy added to Unity by the Initialize method in the previous listing calls the RegisterPublisher and RegisterSubscriber methods of the EventBroker class. This means that, when Unity detects a PublishesAttribute or SubscribesToAttribute in a class that it creates, it automatically registers the class or member as a publisher or a subscriber in the EventBroker.

If the registration is for a publisher, the EventBroker calls the AddPublisher method of the PublishedEvent class. This method adds the new publisher to the list of publishers and wires up an event handler named OnPublisherFiring to the published event. Therefore, when the publisher raises the event, the handler in the PublishedEvent class can iterate through the list of subscriber delegates and invoke each one, as shown in the following code.

private void OnPublisherFiring(Object sender, EventArgs e)
{
  foreach(EventHandler subscriber in subscribers)
  {
    subscriber(sender, e);
  }
}

After you create a custom container extension, you must add it to the Unity container. You can do this by compiling the extension and specifying the type and assembly name in the configuration file for the Unity Application Block. For details of how to configure Unity container extensions, see Entering Configuration Information.

However, the EventBroker Extension QuickStart adds the custom SimpleEventBrokerExtension at run time by calling a method of the Unity container class. The StopLight application Program class that initializes the application and loads the main form creates a new UnityContainer instance and registers the concrete types that map to the ILogger and IStoplightTimer classes. It also calls the AddNewExtension method, specifying the SimpleEventBrokerExtension class, as shown in the following code.

IUnityContainer container = new UnityContainer()
           .AddNewExtension<SimpleEventBrokerExtension>()
           .RegisterType<ILogger, TraceLogger>()
           .RegisterType<IStoplightTimer, RealTimeTimer>();

The container automatically instantiates the extension and calls the Initialize method that you saw in the section Creating a Custom Unity Container Extension of this topic.

As shown earlier in Figure 1, the EventBroker Extension QuickStart uses the custom SimpleEventBrokerExtension container extension to implement the publish and subscribe pattern for two events:

  • The RealTimeTimer class publishes an event named TimerTick that it raises when the timer reaches zero. The StoplightSchedule class subscribes to this event. In the event handler, it updates its index to the lightTimes array (an array of TimeSpan values for the duration of the colors), sets the new duration for the light, and starts the timer running again.
  • The StoplightSchedule class publishes an event named ChangeLight that it raises before it changes the timer duration and restarts the timer. The StoplightPresenter class subscribes to this event. In the event handler, it calls the Next method of the StopLight class to change the color of the light and write a message to the TraceLogger.

To indicate that it publishes the TimerTick event, the RealTimeTimer class uses the Publishes attribute, specifying the name of the event publication, as shown in the following code.

[Publishes("TimerTick")]
public event EventHandler Expired;
private void OnTick(Object sender, EventArgs e)
{
  timer.Stop();
  OnExpired(this);
}

The OnExpired method simply checks that there is an event handler instance and raises the event, as shown here.

protected virtual void OnExpired(object sender)
{
  EventHandler handlers = Expired;
  if(handlers != null)
  {
    handlers(this, EventArgs.Empty);
  }
}

The StoplightSchedule class subscribes to the TimerTick event by applying the SubscribesTo attribute to a suitable event handler.

[SubscribesTo("TimerTick")]
public void OnTimerExpired(Object sender, EventArgs e)
{
  EventHandler handlers = ChangeLight;
  if(handlers != null)
  {
    handlers(this, EventArgs.Empty);
  }
  currentLight = ( currentLight + 1 ) % 3;
  timer.Duration = lightTimes[currentLight];
  timer.Start();
}

The preceding code shows that the OnTimerExpired event handler raises an event named ChangeLight when it receives the TimerTick event. ChangeLight is the local name of the event, but the StoplightSchedule class also publishes the event using this name, as shown in the following code.

[Publishes("ChangeLight")]
public event EventHandler ChangeLight;

Finally, the StoplightPresenter class subscribes the ChangeLight event using an event handler named OnScheduledLightChange. Inside the event handler, it calls the Next method of the StopLight class, as shown in the following code.

[SubscribesTo("ChangeLight")]
public void OnScheduledLightChange(Object sender, EventArgs e)
{
  stoplight.Next();
}

The code examples in this section demonstrate how useful the custom SimpleEventBrokerExtension is for working with distributed events and how easy it is to publish and subscribe to events when using Unity to generate instances of the classes used in the application.

Show:
© 2014 Microsoft