Events

This section covers the unique Events Application Block found in the Enterprise Library Extensions library.
The events block is all about being able to raise and handle events in a disconnected manner, where producer and consumer do not know of each other. Everything is being mediated by the EventManager class.

Additionally, the events block makes it easy to aggregate events to prevent too many events having to be handled. The event aggregation mechanism additionally works as a kind of transaction scope to ensure that no events are raised within a using block if the scope’s complete method was not called. This make for some very interesting possibilities when using command query segregation principle.

The event block works with two concepts: Event producers and event handlers. Event handlers can handle events while event producers can raise events. In the block, producers are implemented in the IEventProducer interface, while event handlers implement the IHandlesEvent interface.

An event in the events block is defined by a type. For example, if you have a system maintaining customers, when changing a customer you could have a CustomerChange object. The event is then said to be of that type. The CustomerChange object could be defined as:

public class CustomerChange
{
  public CustomerChange(int customerID)
  {
    CustomerID = customerID;
  }
 
  public int CustomerID { get; private set; }
}

Having an event type like this means that whenever an event is raised, a new instance of that type is raised and passed to all event handlers with any information desired. To handle an event of the type, create a new event handler like shown next and implement the abstract method Handle.

public class CustomerChangeHandler : HandlesEvent<CustomerChange>
{
  protected override void Handle(object sender, EventArgs<CustomerChange> args)
  {
    int customerID = args.Value.CustomerID;
  }
}

Last edited Apr 18, 2012 at 8:25 PM by RasmusTherkelsen, version 2

Comments

No comments yet.