Skip to content
Andrea Campolonghi edited this page Aug 13, 2010 · 17 revisions

Introduction of CfEventManager ( CFEM )

CFEM aims to be the foundation of your applications and frameworks. It does what the word says … is a central engine that is capable to register events, subscribe listeners and dispatch events.
CFEM works at very low level and is intended to make event programming pattern available in cfml programming practice.

Is CFEM a MVC framework ?

Not it is not. CFEM just takes cares of the event processing but is not intended to manage what the events will be used for. Btw any MVC frameworks should benefit from a central events engine so CFEM can be a very good point to add your personal MVC on top of it.

Is CFEM going to slow down my application ?

No. It is not going to slow down your application. Of course the event processing takes a very small overhead but your applications will be much more extensible than it is without implementing a event programming technique. Of course implementing an event makes sense when you want to add an interception point. Abusing of events can be much more dangerous that not using them at all. So always think if dispatching an event is really what your application need in that point.

When should I dispatch an event ?

Let’s think at this scenarion.

Your application has a method that register a new user.

service.addUser();

The addUser method will make some db interaction and will add the user.

function addUser(){
      ........logic .....
      var myuser =  myfactory.getMyuser(newid);
} 

After a while I decide that when I register a new user I want also to send an email.

 function addUser(){
      ........logic .....
      var myuser =  myfactory.getMyuser(newid);
      mailService.send(myuser);
} 

And then I decide that also some filesystem folder must be created and some logging must be done ….

 function addUser(){
      ........logic .....
      var myuser =  myfactory.getMyuser(newid);
      mailService.send(myuser);
      loggerService.log(myuser);
      fileSystemService.createUserHome(myuser);
} 

The point is that I will going on to modify the addUser method and maybe I will also start to put some conditional code in it cause I want for example the email to be sent only if the user is added to a certain group.
The code becomes difficult to maintain and test and my core.

This is a great point to dispatch and event.

 function addUser(){
      ........logic .....
      var myuser =  myfactory.getMyuser(newid);
      var data = {user = myuser};
      var event = application.cfem.createEvent(name = 'onUserAdd', data = data );
      application.cfem.dispatchEvent(event);
} 

Now all your logic will be demanded to the listener that will subscribe to this event. I will probably add a listener for logging, one for emailing and one for the filesystem. Small bunches of code that makes one single task….easier to maintain and easier to test.