Event Aggregation


Assembly  EnterpriseLibraryExtensions.Events.dll
 Namespace  EnterpriseLibraryExtensions.Events.Aggregation
 Class   EventAggregationScope 


Event aggregation is about scoping events – not only to collate events of the same or similar type but also to control whether or not events in an entire scope should be raised. This is much similar to working with .NET build-in TransactionScope class.

To start an event aggregation scope, initialize a new class in a using statement like this.

using (var scope = new EventAggregationScope())

All events raised on with the IEventProducer will now be scoped – that is, if they are within the using statement. Being scoped means by default, that events will not be raised if the scope is not called with a complete statement before going out of scope like this:

scope.Complete();

As is the case with .NET’s TransactionScope class it is considered best practice to place the complete statement as the last statement in the using block. If EventAggregationScope and TransactionScope has been stacked, call the transaction scope’s complete method before the event aggregation scope’s. This will ensure that if a transaction fails, no events are raised.

Below can be seen a scope with events raised, the scope being completed and therefore raising events.

using (var scope = new EventAggregationScope()) 
{
  scope.Options = new TypeFilteringScopeOptions(); 

  // Raise event!
  
  var eventProducer = container.Resolve<IEventProducer>();
  eventProducer.Raise(null, new SampleEventType("Hello World"));
  scope.Complete();
}

For an EventAggregationScope it is possible to specify options for filtering. The default option for a newly created scope is NoFilteringOptions. A scope’s Options property cannot be null, but to disable any filtering, apply EventAggregationScope.NoFiltering to the Options property.

scope.Options = EventAggregationScope.NoFiltering;

Apart from the no filtering option, the framework supports type filtering – where only the first event of a certain type is raised, and filtering based on comparers. The latter will require comparer instances to be registered on the options class in order to do the filtering but when that is in place, this technique can be more useful than type filtering because it can intelligently determine if two events of the same type are really equal.

To register an intelligent filtering on the event aggregation option, consider the following sample:


using (var scope = new EventAggregationScope())
{
  var options = new FilteringScopeOptions();
  options.RegisterComparer(new SampleEventTypeComparer());
  scope.Options = options;
}

A new options class is initialized and an event comparer is registered on the options object. Afterwards the options object is assigned to the scope’s options property. This will make the scope use the filtering options when events are collated and evaluated.

The SampleEventTypeComparer is defined as:

public class SampleEventTypeComparer : CompareEvent<SampleEventType> 
{
  protected override bool IsMatch(SampleEventType left, SampleEventType right) 
  {
    return string.Compare(left.EventValue, right.EventValue, true) == 0; 
  }
}

Last edited Apr 24, 2012 at 7:36 PM by RasmusTherkelsen, version 4

Comments

No comments yet.