tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/27-executing-commands-directly-from-mediatorsRobotlegs: Discussion 2013-04-28T10:02:56Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T19:14:39Z2010-01-08T19:14:39ZExecuting Commands Directly from Mediators <div><p>One of my pet-peaves of almost <em>all</em> 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.<br></p>
<p>So two questions:</p>
<ul>
<li>
<p>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 <em>ever</em>
pay off?</p>
</li>
<li>
<p>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.<br></p>
</li>
</ul>
<p>Thanks!<br>
-D</p></div>dwabyicktag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T19:25:48Z2010-01-08T19:25:48ZExecuting Commands Directly from Mediators <div><p>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.</p>
<p>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</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T19:29:53Z2010-01-08T19:29:53ZExecuting Commands Directly from Mediators <div><p>I currently don't have the time for an elaborate answer to the
first<br>
questions (and others are far more capable of that than I am,
anyway),<br>
but here's a quick answer to the second one:<br>
- declare the injector as a dependency on your mediator - create
the command with var command : YourCommand =
injector.instantiate(YourCommand);<br>
and execute it with<br>
command.execute();</p>
<p>Also, most of the core contributors to Robotlegs might prefer
to<br>
invoke commands via the event bus, but there's really nothing<br>
enforcing that in the framework at all. For example, Robert
Penner<br>
doesn't use the event bus at all in his projects, so he's
obviously<br>
either not using commands or invoking them by other means.</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T19:38:01Z2010-01-08T19:38:01ZExecuting Commands Directly from Mediators <div><p>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.</p>
<p>In your mediator (or model, or command):</p>
<pre>
<code>[Inject] public var injector:IInjector;</code>
</pre>
<p>Where you want to invoke the command:</p>
<pre>
<code>injector.instantiate(MyCommandClass).execute();</code>
</pre></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T22:04:31Z2010-01-08T22:04:31ZExecuting Commands Directly from Mediators <div><p>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.</p>
<p>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.</p>
<p>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.</p></div>dwabyicktag:robotlegs.tenderapp.com,2009-10-18:Comment/8235442010-01-08T22:10:27Z2010-01-08T22:10:27ZExecuting Commands Directly from Mediators <div><p>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.</p></div>Joel Hooks