tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2353-injecting-mediator-into-command-is-this-possibleRobotlegs: Discussion 2013-06-14T12:21:19Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-11T20:04:48Z2013-06-12T07:14:26ZInjecting Mediator into Command - is this possible?<div><p>Because views and mediators can be many (you might have 3
instances of SomeView, each with a SomeViewMediator) it doesn't
make sense to inject mediators into other objects - after all,
which of the many mediators that meet that injection rule would you
want to inject?</p>
<p>If you really need to pass a reference to the mediator and you
know that it's safe to do so, you can map it in the injector
yourself (in the mediator). Don't forget to unmap it when the
mediator is destroyed.</p>
<p>hth</p>
<p>Stray</p>
<p>On 11 Jun 2013, at 20:38, seansutton <a href=
"mailto:tender+de2056c0c304634dd7016788f27ac04d3f6e6dc01@tenderapp.com">
tender+de2056c0c304634dd7016788f27ac04d3f6e6dc01@tenderapp.com</a>
wrote:</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-12T11:18:38Z2013-06-12T11:18:40ZInjecting Mediator into Command - is this possible?<div><p>Hi Stray. Thanks for your reply :)</p>
<p>Hmmm... so the error I am getting happens EXACTLY because
mediators shouldn't be directly injected into commands? i.e. this
is by design?</p>
<p>I wonder how clear this is for RL users. Some sources I've read
seem to make this a little unclear (though it may be my slow
brain!!):</p>
<p>The RL best practices says : 'commands may ... Be injected with
Models, Services, and Mediators to perform work on directly'.
Should this be revised / is it inaccurate?</p>
<p>Also - unless I missed it - I didn't see this issue discussed in
your RL book. It seemed logical to me to assume that mediators (if
used as singletons) should be retrievable via DI just like models
and events. In my experience, 90% of mediator and view combinations
exist only once and last the duration of the app's existence. Are
my use cases unusual?</p>
<p>... Maybe I'm getting a little distracted by how easy it is to
access Mediators from commands with Pure MVC...</p>
<p>Anyway - back to my original question: Instead of finding and
interrogating mediators in commands, should I instead have the
mediator update a model earlier in the process, and have the
command query the model instead?</p>
<p>This seems a little awkward as there is no real state change to
the app, and it seems fair for the mediator to asked directly.</p>
<p>Thanks again for any further enlightenment!!</p></div>seansuttontag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-12T11:30:16Z2013-06-12T11:30:16ZInjecting Mediator into Command - is this possible?<div><p>Yes, the instructions must be out of date (sorry, not my
department, not that any of it is my department any more - I just
picked up your query in my email). Mediator injection was removed a
couple of years ago when it became apparent that the many-to-many
relationship rendered injection-by-default unstable.</p>
<p>As I said, one single line of code in your mediator makes it
available for injection. Just do:</p>
<pre>
<code>injector.mapValue(MediatorType, this);</code>
</pre>
<p>and that fixes it, as well as signifying clearly to other
developers (or yourself in future) that this mediator is being
accessed elsewhere.</p>
<p>Personally I don't think I've ever injected a mediator anywhere
- unlike PMVC mediators, RL Mediators are meant to be disposable,
exist solely for the purpose of wiring views in to the app events
system and shouldn't have an API that other objects could use
anyway. So, yes, usually I would dispatch data events from the
mediator that update a model somewhere, and have commands query
that model.</p>
<p>But if you choose to use mediators as ViewControllers that's
your choice - it's just that the MediatorMap was never intended to
produce ViewControllers. The various view-related maps in RL2
provide much better support for the ViewController way of working
(either along side mediators or instead of them).</p>
<p>We possibly didn't mention it directly in the book - we were
restricted in terms of page count - but the fact that we did talk
about how your Mediator is just a delivery man and should never
have an API does lean towards there being no point in injecting it.
:)</p>
<p>Anyway, one line of code doesn't seem like much of a hardship -
if your app truly is disposable then the shortcut won't cost you
much, and it's easy enough to refactor later.</p>
<p>hth,</p>
<p>Stray</p>
<p>On 12 Jun 2013, at 12:18, seansutton <a href=
"mailto:tender+de2056c0c304634dd7016788f27ac04d3f6e6dc01@tenderapp.com">
tender+de2056c0c304634dd7016788f27ac04d3f6e6dc01@tenderapp.com</a>
wrote:</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-12T13:22:25Z2013-06-12T13:22:27ZInjecting Mediator into Command - is this possible?<div><p>Hi Stray,</p>
<p>Thank you once again for your detailed reply. I have a much
better understanding now :)</p>
<p>Also - if you state in the book that mediators 'should never
have an API' then this is a strong hint, yes.</p>
<p>Thanks again!!</p></div>seansuttontag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-13T17:28:29Z2013-06-13T17:28:31ZInjecting Mediator into Command - is this possible?<div><p>Ondina I'm sorry to re-open this discussion</p>
<p>I'm using Robotlegs 1.5.2 and there does not seem to be a
straightforward way of getting to the injector from a mediator.</p>
<p>Am I being really stupid? Am I missing something?</p></div>seansuttontag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-13T17:54:13Z2013-06-13T17:54:30ZInjecting Mediator into Command - is this possible?<div><blockquote>
<p>Ondina I'm sorry to re-open this discussion</p>
</blockquote>
<p>Hehe, no problem.</p>
<blockquote>
<p>Am I being really stupid? Am I missing something?</p>
</blockquote>
<p>You’re not stupid!!</p>
<p>You have to inject the injector into the mediator</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-13T17:56:06Z2013-06-13T17:56:06ZInjecting Mediator into Command - is this possible?<div><p>like this:</p>
<pre>
<code>[Inject]
public var injector:IInjector;</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-13T18:17:45Z2013-06-13T18:17:45ZInjecting Mediator into Command - is this possible?<div><p>Doh. You are a super star :) Thanks so much to yourself and
Stray for the quick replies :)</p></div>seansuttontag:robotlegs.tenderapp.com,2009-10-18:Comment/272685382013-06-13T18:28:56Z2013-06-13T18:28:56ZInjecting Mediator into Command - is this possible?<div><p>Haha.. Stray is THE super star! Not me.</p>
<p>You're welcome :)</p>
<p>You can close the discussion yourself.</p></div>Ondina D.F.