By Peter Bell

How ColdSpring Implements AOP using a Decorator

Chris Scott has done an amazing job of adding Aspect Oriented Programming features to ColdSpring.

During the AOP BOF session last week at CF United, he explained how he’d implemented AOP using a decorator. It’s a great example of a practical use of the design pattern and may be a good primer for anyone who wants to consider other ways of implementing AOP in CF.

Basically, a decorator is a class that wraps another class, exposing the same interface, but adding additional behavior. From the GoF summary, the decorator allows you to "attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality".

Why would I care? Well, imagine that you have a bunch of classes you'd like to add logging to. You want to run some simple logging code on the execution of each method (or all methods matching a certain criteria). You could add the code to each method, but that lowers cohesion (why should a user service have to know about logging) and decreases maintainability (what happens when you want to change the way you do your logging).

With a decorator, you can wrap the class, replicating, intercepting and extending each method call. Conceptually the code would be something like:

<!--- In the class --->
<cffunction name=”doSomething”>
CODE TO DO SOMETHING
</cffunction>

<!--- In the decorator --->
<cffunction name=”doSomething”>
<cfscript>
class.doSomething(arguments);
logclass.logCall(arguments);
</cfscript>
</cffunction>

In ColdSpring, there are various AOP methods which can add code before, after, around and/or on error to a set of methods, but the basic principle is that ColdSpring takes your class (say UserService) and automagically wraps it with another proxy class exposing the same interface (methods, arguments and return types) so the proxy gets called by your code and can add your advice (that is the code you want to add to the various methods) before, after or around the original method call.

ColdSpring does a great job of this, so I can’t think why you’d write this yourself, but I wanted to write an article on creating an "in class decorator" using mixins and figured this would be a useful backgrounder.

Please check out the framework and let me know what you think. Wherever you have cross cutting concerns (logging and security are the obvious examples, but workflow is another example) it is a great solution to consider. Dave Ross even showed an example in his ColdSpring presentation of a caching system that allowed you to switch off your database and keep your application running using AOP - very cool indeed.

Comments
Hey Peter, really good post, but I though I should clear up a little misconception. I wouldn't necessarily say that ColdSpring uses a decorator to implements AOP. A decorator is an object that implements the same interface as another object and adds functionality around method calls to it, thus 'decorating' it's functionality. Although AOP, when implemented with method interceptors, creates a similar effect, it also tries to overcome the limitation of having to create concrete implementations of every object that you want to decorate, a shortcoming of the Decorator pattern. This is done by creating a runtime proxy through dynamic compilation, one of the many treats of coldfusion in general. In actuality, we all benefit from dynamic compilation, we save a file and it is automatically compiled and linked in a running j2ee application, quite cool indeed. What is so cool about using AOP is, you do most of you work by writing very reusable components that tie directly into the method interceptor infrastructure, and the frameworks handles the proxy generation for you. In practice your components become for more generalized and reusable than if you were to use decorators.
# Posted By Chris Scott | 7/4/06 8:10 PM
Hi Chris,

Many thanks for the clarification! Looking forwards to playing with this stuff in the near future!

Best Wishes,
Peter
# Posted By Peter Bell | 7/5/06 8:50 AM
They had joined[url=http://www.game4power.com/]game4power[/url] the humans, elves, dwarves, and [url=http://www.game4power.com/]Buy Wow Gold[/url]dragons to decimate the demonic warriors and ghoulish beasts and push the[url=http://www.aocsale.com]aoc gold[/url] remnants back into [url=http://www.game4power.com/buy-gold/" target="_blank">http://www.game4power.com/buy-gold/]wow gold cheap[/url]the hellish beyond. Thousands [url=http://www.vipwarhammergold.com]warhammer gold[/url]had perished, but the alternative… The dragon mage snorted. In truth, [url=http://www.itemchannel.com]wow gold[/url]there had beenno alternative. Krasus waved [url=http://www.aion4gold.com]aion gold[/url]long, tapering fingers over the orb, summoning a vision of the orcs. The view[url=http://www.itemchannel.com]world of warcraft gold[/url]blurred momentarily, then revealed a mountainous, rocky [url=http://www.ppgold.com]wow gold cheap[/url]area further inland. A harsh land, but one still full of life and capable of [url=http://www.wowgoldone.com/]cheapest wow gold[/url]supporting the new colonists. Already, several stone [url=http://www.wowgoldone.com/][i]cheap wow gold[/i][/url]structures had risen in the main settlement, where the [url=http://www.vipaiongold.com]aion gold[/url]Warchief and one of[url=http://www.aionkina.com]aion kina[/url]the heroes of the war, Thrall, ruled. The high, rounded edifice [url=http://www.game4power.com/]Wow Gold [/url],[url=http://www.game4power.com/]www.game4power.com[/url]that served as his quarters was crude by the standards of any other race, but orcs had a propensity toward basics.
# Posted By buy wow gold | 6/27/09 5:11 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.