Cairngorm – Commands and Responders

Recently, I’ve seen a few questions around Cairngorm and how commands and responders fit together when using asynchronous services with Flex. I’d like to clarify a couple of points.

A Command need only implement the Flex IResponder interface if it is to be the responder for an asynchronous service call.

This is probably obvious to many, but I’ve seen Command classes which implement IResponder, but don’t do any ansychrnous service calls and have empty result() and fault() methods. In this cases, the Command should implement the ICommand interface only and not the IResponder interface.

The Command class does not have to be the responder for asychronous service calls.

I’ve not seen many implementations like this, but the Cairngorm command pattern is designed in such a way that your responder can be an instance of another class, as in this example:

public class AddTaskCommand implements Command
{
public function execute( event : CairngormEvent ):void
{
var addTaskEvent : AddTaskEvent = event as AddTasksEvent;
var task : Task = addTaskEvent.task;
var addTaskResponder : AddTaskResponder = new AddTaskResponder( task );
var delegate : TaskDelegate = new TaskDelegate( addTaskResponder );
delegate.addTask( task );
}
}

public class AddTaskResponder implements IResponder
{
private var task : Task;
public function AddTaskResponder( task : Task )
{
this.task = task;
}
public function result( event : Object ):void
{
//handle the result here
}
public function fault( event : Object ):void
{
//handle the fault here
}
}

This implementation allows better separation of logic and potential reuse of responders, and also makes things easier to test.

7 Responses to Cairngorm – Commands and Responders

  1. Sherry says:

    Hi, do you know if QTP with Flex plug-in work with Cairngorm? I was able to record scripts with FlexStore, but not our own application with Cairngorm.
    thanks

  2. Kono says:

    Thanks!
    I have posted my recent work about Cairngorm on my blog http://www.moorwind.com/read.php?65 .
    Cairngorm is great. I used it in this project and it helped me write code clearly. So I decided to share the source. This project is a web manage system(with JSP) and it’s still have not finished. So,I will write a quick guide about develop Cairngorm (In Chinese, code commented in English) when I completed this project.

  3. HI Sherry,
    Yes, QTP should work with Cairngorm, which is just a few actionscript classes. Have you made you app fully automatable?

  4. Gazeteler says:

    i’m new to Cairngorm. Reading blogs always. Hope i will help people like you.

  5. Manu Dhanda says:

    Hii,
    Is there any particular reason to keep the var ‘task’ in your responder?
    As we have access to it (as in addTaskEvent.task) in our command class and we can use it in our delegate as it is, so why do we need to pass it on to responder?
    Thanks.

  6. ask siirleri says:

    Hi, do you know if QTP with Flex plug-in work with Cairngorm? I was able to record scripts with FlexStore, but not our own application with Cairngorm.
    thanks

  7. Shreyas says:

    Hi Alistair,
    I have been stuck in a caringorm problem for quite some time, and not able to get any response from anyone. Hope you can help me out.
    Scenario:
    Cairngorm 2 framework used.
    The Application loads different modules at runtime on user selection.
    The Application and each of the modules has a seperate front controller.
    One of the module it loads is using getDefinitionByName()[In the command through the controller] to instantiate an object at runtime. The class it is trying to instantiate has been imported along with other dynamically instantiable classes in a seperate AS named ClassDeclarations.
    This ClassDeclaration is instantiated at the Module so that the references of the dynamic classes are available and compiler includes the same in the swf.
    Problem:
    But this doesnt work. It gives an
    ReferenceError: Error #1065: Variable XXX is not defined.
    at global/flash.utils::getDefinitionByName()
    at…
    Observations:
    1. Now if the same ClassDeclarations are instantiated at the Application level, the one which loads the module, it magically works.
    2. When the module is loaded, and the event is dispatched, the front controller of the module is able to route the event to its command properly.
    Questions:
    I wonder why? and what is the difference it makes to put the ClassDeclarations at Application level than at the module level? Since the classes in ClassDeclaration are used only by the module, it is not making sense to to put it at the Application level and increase the App size. The front Controller and hence the command at the module level is some how not able to access the Classes specifically used in the Module. Is it anything to do with how memory is shared in Flex VM? Any way to achieve the same?