tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/23-inject-in-views-how-to-and-whether-toRobotlegs: Discussion 2013-04-26T17:09:44Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/7189362009-12-13T21:54:24Z2009-12-13T21:54:24Z[Inject] in Views - How to and whether to?<div><p>Hi Steven,</p>
<p>The [Inject] metadata has little to do with Actor (or Robotlegs
in fact). It has everything to do with objects that are managed by
a Dependency Injection container (SwiftSuspenders by default) -
which relates to "mapped as a singleton or class in a context" as
you mentioned. The DI library has to have a rule for the
dependency, and it has to be told to inject into your target class
(the injectee). Robotlegs does all of this for you when it comes to
Mediators and Commands.</p>
<p>The purpose of the Mediator is to remove all application
specific knowledge from a view component. But what I would do in
your case is expose a method on the view component that accepts
some kind of reference (the dependency), set up the dependency for
injection on your mediator, and pass that reference through to the
view in the mediator's onRegister hook. Which I think is what you
are doing already.</p>
<p>If you're not using Mediators, then you could use the ViewMap to
handle injection directly into view components as they arrive on
stage. For example, inside MyComponent:</p>
<pre>
[Inject]
public var myDependency:SomeDependency;
</pre>
<p>In your context, or in a command:</p>
<pre>
injector.mapSingleton(SomeDependency);
// .. //
viewMap.mapType(MyComponent);
</pre>
<p>and then, later:</p>
<pre>
addChild(new MyComponent());
</pre>
<p>You <em>could</em> use both the MediatorMap and the ViewMap, but
that seems like overkill to me - and has performance tradeoffs.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/7189362009-12-13T22:03:12Z2009-12-13T22:03:14Z[Inject] in Views - How to and whether to?<div><blockquote>
<p>The purpose of the Mediator is to remove all application
specific knowledge from a view component.</p>
</blockquote>
<p>This pretty much answered the question that I shouldn't do it.
The rest of the information is good to know, as well.</p>
<p>Thanks!</p></div>stevensackstag:robotlegs.tenderapp.com,2009-10-18:Comment/7189362009-12-13T22:29:04Z2009-12-13T22:29:04Z[Inject] in Views - How to and whether to?<div><p>Hah, I suppose the answer should have been: If you are using the
Mediator pattern then it is <em>not</em> appropriate to give
application-scope instances to your view components.</p>
<p>But that's only if you use the Mediator pattern. Some developers
prefer the Presenter pattern - which roughly boils down to
injecting Presenters directly into your view components - or
variations thereof.</p></div>Shaun Smith