Commands implementing IResponder

Navaid Faiz's Avatar

Navaid Faiz

04 Nov, 2009 05:32 PM

Coming from Cairngorm to Robotlegs - My question is if it is a good idea to have Commands implement IResponders.
What could be the possible issues by doing so.

package com.xyz.selector.controllers
{

// Imports Here

public class GetCustomerCommand extends Command implements IResponder 
{
    [Inject]
    public var event:GetCustomerEvent;

    [Inject]
    public var customerModel:CustomerModel;

    public function GetCustomerCommand()
    {    
    }

    override public function execute():void
    {
        var customerService:CustomerService = new CustomerService(this);            
        injector.injectInto(customerService);

        customerService.getCustomer(event.customerID);
    }   

    public function result(event:Object):void
    {
        try
        {   
            var result:CustomerVO = event.result as CustomerVO;
            this.customerModel.currentCustomer = result;                    
        }
        catch(ex:Error)
        {                   
        }
        finally
        {               
        }
    }

    public function fault(event:Object):void
    {
    }
}

}

Thanks for a great framework!!
-Navaid

  1. Support Staff 1 Posted by Joel Hooks on 04 Nov, 2009 05:37 PM

    Joel Hooks's Avatar

    As a matter of personal taste, I think this sort of asynchronous work doesn't belong in a Command. A Command should do its work and die. In the above, I wouldn't make a Command a dependency of the Service. Instead I would register the service and inject it into the command. Then you can simply access the methods on the service. The service would be the responder, and dispatch an event that would notify other Commands and/or Mediators that something has occurred.

    To me, if in describing your Command you use the word "and" then the pattern is being broken. A Command should do one job and die.

    That said, if this is how you like to work, there is certainly nothing holding you back.

  2. 2 Posted by Navaid Faiz on 04 Nov, 2009 06:24 PM

    Navaid Faiz's Avatar

    Thanks for the quick post Joel. I initially started with injecting the service in the command.

    I started without IResponder:
    1. GetCustomer event gets dispatched from Mediator
    2. GetCustomerCommand then calls CustomerService method
    3. CustomerService calls and then retrieves the customer data. This customer data still needs to be worked on before Mediators gets it. So CustomerService dispatch ProcessCustomer Event.
    4. ProcessCustomerCommand then updates the CustomerModel. That it turn dispatches the ProcessCustomerCompleteEvent which is then monitored by the Mediators.

    I ended up creating far too many ProcessxxxxxCommands and mappings of commands to the events. Injecting a model in a service is not recommended either I believe.

  3. Support Staff 3 Posted by Joel Hooks on 04 Nov, 2009 06:34 PM

    Joel Hooks's Avatar

    I think it would be much better to inject a model/service into a Mediator than to start down the path of heavy async commands.

    The docs need to be updated to reflect this. Decoupling is fine and good, but in many cases coupling provides clarity and intent.

  4. Support Staff 4 Posted by Joel Hooks on 04 Nov, 2009 06:43 PM

    Joel Hooks's Avatar

    You could also inject the model into the service. This is coupling but
    if you were to do it with an interface it wouldn't be that bad.
    Definitely makes the service less portable, but if that isn't an
    issue...

  5. Support Staff 5 Posted by Shaun Smith on 07 Dec, 2009 02:57 PM

    Shaun Smith's Avatar

    Just to clarify:

    Commands should be short-lived. It is worth noting that nothing holds onto a Command instance, so if you make it a Responder, it might be eaten by the Garbage Collector before getting a chance to handle its own response.

    Services should handle asynchronous operations. Services can be injected directly into Mediators, and Mediators can invoke methods on those Services. Models can also be injected directly into Mediators, but only for the purposes of "reading" data - it would not be a good idea to manipulate a Model from a Mediator.

    I'm going to mark this issue as Resolved for now, but please feel free to re-open it if you feel it hasn't been addressed properly.

  6. Shaun Smith closed this discussion on 07 Dec, 2009 02:57 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac