Executing Commands Directly from Mediators

dwabyick's Avatar

dwabyick

08 Jan, 2010 07:14 PM

One of my pet-peaves of almost all the AS3 framework architectures is that they force you to execute commands via an event mapping. While I understand, and wholeheartedly agree, why models and services should emit events, I don't think I care of a mediator has a direct reference to a command.

So two questions:

  • Why do frameworks like Robotlegs strongly suggest / enforce that you have invoke commands via events? What use case am I missing? I get that it decouples, but when does this decoupling ever pay off?

  • Assuming I don't like the previous answer, what's the easiest way to bend Robotlegs so that I can execute commands directly from mediators? I figure I need to have some way to make the framework inject dependencies.

Thanks!
-D

  1. Support Staff 1 Posted by Joel Hooks on 08 Jan, 2010 07:25 PM

    Joel Hooks's Avatar

    For smaller commands or simply to create a controller tier API in my apps I extend Actor and create a MyAppAreaController class that has command-like methods. Some of these methods will make simple calls to service/model tier, while others might invoke execute on a command instance.

    To get at commands via a mediator you'd simply need to create the command instance, provide any dependencies it needs, and touch execute(). SwiftSuspenders could help in this regard. Now you can have fun with your hard-coupled commands inside of your mediator ;P

  2. Support Staff 2 Posted by Till Schneidere... on 08 Jan, 2010 07:29 PM

    Till Schneidereit's Avatar

    I currently don't have the time for an elaborate answer to the first
    questions (and others are far more capable of that than I am, anyway),
    but here's a quick answer to the second one:
    - declare the injector as a dependency on your mediator
    - create the command with
    var command : YourCommand = injector.instantiate(YourCommand);
    and execute it with
    command.execute();

    Also, most of the core contributors to Robotlegs might prefer to
    invoke commands via the event bus, but there's really nothing
    enforcing that in the framework at all. For example, Robert Penner
    doesn't use the event bus at all in his projects, so he's obviously
    either not using commands or invoking them by other means.

  3. Support Staff 3 Posted by Shaun Smith on 08 Jan, 2010 07:38 PM

    Shaun Smith's Avatar

    MVC discourages the idea of views directly manipulating models. But, as Joel and Till have mentioned, RL doesn't stop you from doing anything that you want to do.

    In your mediator (or model, or command):

    [Inject] public var injector:IInjector;
    

    Where you want to invoke the command:

    injector.instantiate(MyCommandClass).execute();
    
  4. 4 Posted by dwabyick on 08 Jan, 2010 10:04 PM

    dwabyick's Avatar

    Hey guys, Thanks for all the answers. That seems 90% of the way there. However, I do need to pass an argument to the Command, which Robotlegs does seem to do nicely. Maybe I can peek in the code, and see how they do so.

    Joel - I also like your Controller suggestion. I used to use that pattern, before I ever adopted a 'real' AS3 framework. Perhaps I'll go with that again.

    This has got to be the best part of Robotlegs - it helps providing the mechanics to develop a framework how one wants, without getting dogmatic about it. An aside - I'm excited about Penner's work with Signals and Robotlegs. I'm planning to check that out next project.

  5. Support Staff 5 Posted by Joel Hooks on 08 Jan, 2010 10:10 PM

    Joel Hooks's Avatar

    Commands are injected with mapped values. Using either of the instanciation approach Till and Shaun outline will inject any dependencies you want. Additionally, you aren't limited in any way to using the supplied Command class so your commands can implement any interface you choose to accept constructor arguments in the execute method.

  6. Stray closed this discussion on 13 Feb, 2011 06:35 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