Saturday, April 29, 2006

Event Manager Code - Possible Enhancements

Now that I have posted the code for the Event Manager that I have used in several projects, I would like to list some possible enhancements that I have considered:

Precompile regular expressions
The Event Manager supports wildcards. The .NET RegularExpression classes are used to perform matching of a published topic with a wildcarded subscription. Howver, I do not pre-compile the regexps. This is a very simple change to make.

Enhanced regular expressions
We can expand the regular expressions that wildcarded subscriptions will accept. For example, to subscribe to an insert or delete action for a trade, we should be able to have a wildcard topic that looks like this:

Stronger typing of args for event declarations
The third argument of EventManager.Fire() is anything that derives from EventManagerArgs. We can add more type safety by specifying the subclass of EventManagerArgs that a publisher expects to send and that the corresponding subscribers expect to receive.

Microsoft's CAB has the notion of event scoping. We can limit a published event to subscribers that run in the same thread (or a certain thread) as the publisher. We can limit the scope to a named "applet". Otherwise, by default, the event is published globally.

I am not totally thrilled with the fact that, for every object that publishes or subscribes, a call to EventManager.Register(this) is called. The Register() function will examine the passed class, using reflection on the class to build up a dictionary of publishers and subscribers to a topic.

Dynamically register the publisher and subscriber
Instead of using the static way of decorating a method, we may want to add publications and subscriptions dynamically. For instance, we should be able to say something liek this:

EventManager.Subscribe("Magmasystems.TradingSystem.Trade.*", this.OnTradeEvent);

Sinks for event publishing
Implement Sinks so a class can have a crack at an event args before it is sent to the subscribers.

©2006 Marc Adler - All Rights Reserved

1 comment:

Mike said...

Thanks for posting the code. It's a great resource, i've been thinking of how to do something similar when I came across your code. Seems like we have similar ideas. Thanks and nice work.