Steven Webster: Cairngorm 2 (for Beta 3) now has Stateless Commands

« Cairngorm 2 for Flex 2 Beta 3 | Main | Want to work for the Apollo Team ? »

May 09, 2006

Cairngorm 2 (for Beta 3) now has Stateless Commands

We just realised that the build of Cairngorm 2 that we just put out, actually sneaks the "stateless commands" that we've been using ourselves for quite some time now. This is going to require the tiniest amount of change to your Cairngorm applications in a single place - your concrete front controller - and you should understand what benefits we felt motivated this change.

Typically, in your own Front Controller in Cairngorm applications, you would have a bunch of lines of code such as:

addCommand( MyController.EVENT_LOGIN,  new LoginCommand() );
addCommand( MyController.EVENT_LOGOUT, new LogoutCommand() );

We're now going to ask you to change those lines of code to:

addCommand( MyController.EVENT_LOGIN,  LoginCommand );
addCommand( MyController.EVENT_LOGOUT, LogoutCommand );

Since the change is so subtle, let me emphasise - the addCommand() method is now accepting a String with the *name* of the command, rather than a new instance of the command - new LoginCommand() - in its argument list.

What this means, is that each time a command has its execute() method invoked by the controller, this execute() occurs on a new instance of the Command class every time. The handlers (results and faults) are still of course invoked on the instance of the Command that was "executed".

Until now, Cairngorm has always executed on the same instance of the command - something that we wished had never made it into our first release, but once it was out there, we were wary of making the changes.

You should be aware that if your application is relying upon a side-effect between invocations of the Command, then this side-effect will no longer exist between command invocations. So - no state is held in a command between invocations, and if you need to retain state, your strategy should be to explicitly capture that state and hold it in the ModelLocator.

This is still a beta release of Cairngorm, so we're open to suggestions from the community as to this change. However, it's a practice we've been employing for several projects now, and one we'd certainly advocate to our own clients.

Let us know how you get on with this - it's certainly a relief for us that this is finally part of the public build of Cairngorm.

Posted by swebster at May 9, 2006 01:33 PM

Trackback Pings

TrackBack URL for this entry:
http://weblogs.macromedia.com/mtadmin/mt-tb.cgi/7400

Comments

Hi Steve,

I might be missing something but above you mentioned the 2nd arg now being Strings, but should it not be Function refs?

Posted by: Richard Leggett at May 9, 2006 02:13 PM

*sorry Class refs (not Function refs)

Posted by: Richard Leggett at May 9, 2006 02:14 PM

Richard - my mistake, yes. The signature for addCommand now accepts a reference to a Class as follows:

public function addCommand( commandName : String, commandRef : Class ) : void

Posted by: Steven Webster at May 9, 2006 02:21 PM

Nice one -- this is how Commands are implemented in Arp too :)

Posted by: Aral Balkan at May 9, 2006 02:44 PM

I did this in v1 because I couldn't wait.

Now that it is in Flex 2, thank you so much!

Posted by: JesterXL at May 9, 2006 02:45 PM

Hey Aral ... good to hear from you ! It's a relief to be finally aligned with the stateless/reflection-based J2EE implementation we had pre-Flash. Anyway, you can give us the stateless Commands; Cairngorm gave you the Front Controller, Command, Business Delegate, Service Locator, Value Object and most recently the ModelLocator after all ;-) Go on, add a data attribute to ARP Commands execute() method , you know you want to... ;)

Disappointed as well to see that aralbalkan.com doesn't have your hands on it anymore, that was cool... :)

Posted by: Steven Webster at May 9, 2006 02:51 PM

ask and ye shall receive Jesse ;) We've been wanting to do this since Cairngorm 0.99, but were concerned that some people might be inadvertantly relying upon the side-effect of "stateful" commands. We decided we'd keep the migration until a "significant release" of Cairngorm. It's what *we* have been using for a long time now as well !

Posted by: Steven Webster at May 9, 2006 02:53 PM

Hey Steven, I wasn't aware that those design patterns had been developed in Cairngorm -- thanks for enlightening me. Looks like we've sure got a lot of re-educating to do for the Smalltalk and Java people! ;)

Seriously, though, it's a good move and I hope to have more than just my hands on the site in the near future (tsk, tsk, what *are* you thinking?) :)

Posted by: Aral Balkan at May 9, 2006 03:07 PM

Though Aral's comment about Java/Smalltalk is tongue in cheek, it's important that I address this for those that don't know the history behind Cairngorm.

We've always been open and clear in every publication we've made about Cairngorm - whether in Reality J2EE, ActionScript 2.0 Dictionary, Developing Rich Clients for Macromedia Flex, or the numerous conference talks we've given on Cairngorm - that Cairngorm was an innovation that we made upon the work that was published by Alur, Crupi and Malks (Core J2EE Patterns). Our innovation was the selection of a subset of those patterns that we felt were applicable to RIA (and more recently, we've innovated new patterns to the catalogue, most notably the ModelLocator), and in moving these patterns away from the request/response paradigm of J2EE web-application development, to the event-driven command and controller approach we adopted in Cairngorm.

I am a huge believer in crediting the work and ideas that we have built upon, and I don't want to be anything but open and honest about where the inspiration for Cairngorm lies. The team at Sun who presented the Core J2EE Patterns at Java One, wholeheartedly deserve that credit, for their contribution to the J2EE community that provided the foundation research for the Cairngorm RIA microarchitecture.

If we can help that community, through Cairngorm, by lowering the barrier to entry to Flex development, and if we can offer leadership and best-practice to others facing the challenge of building large RIA with Flex, then we have achieved some of our significant aims of Cairngorm.

Posted by: Steven Webster at May 9, 2006 04:02 PM

What else are you guys using today that you're not telling us about yet? ;-)

Posted by: Hans at May 9, 2006 05:57 PM

Aral has done a lot for the community packaging the J2EE design patterns with a nice red bow to encourage Flash developers to use best practices in rich application development.

However my loyalty will always remain with Steven Webster and Alistair McLeod. They are the innovators or applying the J2EE (Alur, Crupi, Malks) and their own design patterns to the brand new world of Rich Internet Applications using Flash. They are the pioneers. If Aral Balkan did not have the of exposure of working with our pioneers a few years back he would probably never had the vision to package ARP. ARP and Cairngorm are so similar it’s ridiculous.

If Steven is a huge believer in crediting the work and ideas that they have built upon, then I am a huge believer of crediting the work and ideas that Steven and Alistair have worked on.

Go Cairngorm!

Posted by: Nathan Vale at June 5, 2006 01:58 AM

The above code correction fix one of the problems I was having, thanks :) However I'm still get this:

code:
public class LoginCommand implements Command, Responder

error:
Interface method onFault in namespace org.nevis.cairngorm.business:Responder is implemented with an incompatible signature in class org.nevis.cairngorm.samples.login.commands:LoginCommand.

Any thoughts as to why?

Posted by: Russell Munro at June 8, 2006 06:42 AM

Post a comment




Remember Me?