Wednesday, May 22, 2013

Domain Events - Getting Visibility

This is my third post about events, you can read the the first one here.

One issue (of many) with this implementation of eventing is getting visibility into what events got auto-registered, and what handlers are wired up correctly.  It is difficult and time consuming to look through logs and step through the debugger, so lets create a quick tool to give us some visibility.  In addition, this tool can also be used as an endpoint for automated functional tests.

Because I am a big MVC fan, I created a controller just for this.  If you are not using MVC, you can create a WCF endpoint, or an ASP.Net webform.

The first thing I did was to modify my IEventer interface and add a couple of methods:


These methods do a couple of important things for us:
  1. GetEventsFromAssemblies - This method will return all IEvent's from the specified assemblies.  Please note the code duplication from the auto-registration code and refactor accordingly.
  2. GetHandlersForEvent - Given a collection of IEvent's, this returns the Type's that handle the event.  Because the IEventer implementation uses our IoC container, we query the container to verify registration.
Now lets take a look at the controller:


The controller gets all the IEvent's from the current AppDomain's assemblies, then gets all the handlers for each one.  It serializes these into json and returns it.  I use a custom ActionResult that sets the ContentType and allows me to use any json serialization library:


When viewing the results, I like to use the JsonView extension for Chrome or Firefox to make the json more readable.