Tuesday, May 21, 2013

Domain Events - A Controller To Raise Events

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

There might be a case where you want to raise an event from outside of your process.  Some examples of this is for functional testing, and manually invalidating cache.

The most difficult part of this is how to handle any type of IEvent.  In order to do this in MVC3, we need to use a custom model binder.  The one below uses Json.Net, and you can easily use whatever Json library you want. You can this generic Json binder for other types besides IEvent.
Now that we have a custom binder, we can go ahead and create our controller. It needs some fancy reflection to be able to call the Raise%lt;T%gt; method correctly.

So to raise an event, just make a HttpPost to this action with your serialized json event, and make sure that set your content-type to "application/json". I like using the RestClient browser plugin to help out with these. Here is an example of the json object: