Thoughts on Event Driven Programming

What is Event Driven Programming? In the context of ColdFusion, the way I see it is as a way to loosely couple different components from each other by announcing when important moments happen (events) in an application and letting other components register interest and then receive notification via some kind of even manager. This way, components do not really know about each other, only about events that may happen in the system and only have the even manager as it’s dependency rather than any number of related components.

Does that make sense?

Just recently I began thinking about event driven programming in the context of ColdFusion. The idea is very appealing to me. Being able to have many of my components be loosely coupled and not having to worry about injecting as many dependencies seems quite nice. Not to mention being able to very easily add new features to an application without having to inject some new component into a bunch of existing components or having to mess with AOP seems lovely.

The trick though is how to go about creating an event driven architecture (EDA) and then how far to take it in an application. Do you try to use EDA to completely decouple every component or just use it for auditing and similar cross cutting concerns. For instance, is it possible to decouple a DAO from a service so that a service merely announces an event and the DAO registered to listen for that event takes action, saving or querying? It seems that saving is one matter since it doesn’t typically involve any return value. But what about querying? Obviously the DAO would need to return results but how would that be accomplished?

This leads to another question. What happens if an event fails? Or succeeds for that matter? Is it possible to notify the original component and should I? I have no idea. It seems that might be pretty tricky. Asynchronous events seem much easier to handle.

The place I really see event driven architectures succeeding though is with things like sending emails, logging, auditing, etc. Crosscutting concerns that need access to many facets of an application. This poses the question though: what about AOP in matters like this? There seems to be some overlap in the two areas. But, as is always the case, it seems some times using an event driven method would be better and likewise, sometimes using an AOP method would be better. I’m curious to know what other uses there are that would be a good fit.

As with any new thing, it is possible to go completely overboard and overuse it, but I’m leaning to a more subdued approach for the time being. Using events to drive things like auditing seems like good approach for now until I understand it more. I am however, excited to seem what new uses I can come up with.

I’ve been taking a look at Sean Corfield’s Edmund framework and it is pretty interesting. I’m definitely going to investigate it further, work up some tests, and see how it goes.