tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/419-injection-fails-into-non-singleton-modelRobotlegs: Discussion 2013-04-28T09:54:17Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-10T02:48:14Z2011-11-10T02:48:18ZInjection fails into non-singleton model<div><p>I am trying to inject a signal into a model that is not a
singleton.</p>
<p>in my context I have:</p>
<p>signalCommandMap.mapSignalClass(SetBrailleFocus,
BrailleSetFocusCommand);</p>
<p>In the model (which is called TabManager) I have:</p>
<p>[Inject] public var setBrailleFocus:SetBrailleFocus;</p>
<p>and then later:</p>
<p>setBrailleFocus.dispatch(boss);</p>
<p>Whenever execution reaches that last line of code, I get "Cannot
access a property or method of a null object reference."</p>
<p>I have tried</p>
<p>injector.mapClass(TabManager, TabManager);</p>
<p>even though that doesn't make sense because I'm not trying to
inject TabManager into anything.</p>
<p>I have also tried making TabManager extend actor.... I'm
grasping at straws, I guess.</p>
<p>What am I missing, here? It all seems straightforward
enough...</p></div>ndechmantag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-10T03:19:24Z2011-11-10T03:19:24ZInjection fails into non-singleton model<div><p>How are you creating the instances of TabManager?</p>
<p>For dependencies to actually be injected, you have to either use
the<br>
injector to create the instance itself, or use injectInto to
satisfy<br>
the dependencies afterwards:</p>
<p>var tabManager : TabManager =
injector.instantiate(TabManager);<br>
or<br>
var tabManager : TabManager = new TabManager();<br>
injector.injectInto(tabManager);</p>
<p>The injector can't magically find out that there's a new
instance with<br>
an [Inject] tag being created somewhere, so you have to help it a
bit<br>
;)</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-10T04:53:59Z2011-11-10T04:54:02ZInjection fails into non-singleton model<div><p>Thanks for your response!</p>
<p>That makes perfect sense, and I had suspected it, but I do not
know how to get the injector out to where these things are being
instantiated.</p>
<p>Almost every view instantiates one, and the instantiation looks
like:</p>
<p>tabManager = new TabManager(this, cancelationButton);</p>
<p>So, I guess that is the question, now.... How do I get the
injector to run in a view?</p>
<p>I could make a wrapper for TabManager, I suppose... then would
that wrapper have to extend SignalContext?</p></div>ndechmantag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-11T02:12:49Z2011-11-11T02:12:50ZInjection fails into non-singleton model<div><p>I could really use an answer to this....</p>
<p>Passing injector from context to controller to view seems
anathema to the aims we're trying to achieve with dependency
injection.</p>
<p>And there are so many views I'd have to have that injector
evrywhere!</p></div>ndechmantag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-11T10:31:07Z2011-11-11T10:31:07ZInjection fails into non-singleton model<div><p>Hi there - I agree, passing it around manually would be a mess.
But the mess is partly there because you're creating something
that's not really a view <em>inside</em> your other views.</p>
<p>You've broken the MVC encapsulation (or at least bent it) and so
the Robotlegs MVCS trousers aren't automatically set up to deal
with this situation.</p>
<p>Luckily Robotlegs is pretty flexible.</p>
<p>There are several ways of approaching this:</p>
<p>1) To re-architect - because it feels to me like a broken
architecture is the problem. But this is pretty radical. So, let's
assume that you're not going to do that.</p>
<p>2) To use the viewMap and wire the TabManager for injection,
assuming that it's a view object and lands on the stage - if it
isn't then that's not going to work. Even if it is, using view
injection and mediators in RL1 together incurs a serious
performance hit.</p>
<p>3) To use a conventional static accessor to get to the Signal
(at least until you figure out something else).</p>
<p>4) If TabManager is a display object then you can always mediate
it and use the mediator to pass the signal.</p>
<p>5) Assuming that it's <em>not</em> a display object, you could
mediate the objects that are creating TabManager and pass the
signal (not the injector) into the mediator, and set up your
TabManagers there.</p>
<p>6) Better than 5 - make a little factory (Singleton) that
creates the TabManagers where they are required. Mediate the views
that need them. Use the factory in the mediator. The
tabManagerFactory should track whether one has already been created
or whether this is a new request, and either supply the new one or
not:</p>
<pre>
<code>tabManagerFactory.createTabManagerFor( view )</code>
</pre>
<p>Basically creating an application-level model <em>inside</em>
multiple views in your view tier is a non-conventional approach to
MVC. You would probably be better simply dispatching a custom event
from your views, not using signals, and then the custom event can
bubble up and be caught in a mediator and dealt with at the
application level.</p>
<p>Signals are great for many situations - but this is a situation
in which I think Events win, because bubbling avoids so many wiring
problems.</p>
<p>hth,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-11T13:02:44Z2011-11-11T13:02:44ZInjection fails into non-singleton model<div><p>Haha! I <em>did</em> call this thing a model in my original
post! I think I did that because I had just spent several hours
reading every forum I could find on injection, ending up with
searching something or other about model injection.</p>
<p>This is not a model, this is probably more correctly referred to
as a controller utility. It handles tab order and whatnot for
views.</p>
<p>But, that said, it is a controller-ish thing that is
instantiated from a view instead of vice versa, so it is not quite
perfect MVCS. It <em>should</em> be instantiated from the
mediator.</p>
<p>This makes me doubly appreciative of your response, Stray, and
also gives me respect for this community. Where else could I have
expected anything but an unhelpful snarky response? But instead you
took the time not only to point out the error in my post and the
ideal correct solution given that error, but also to provide very
helpful solutions in case I did not want to correct the apparent
problem.</p>
<p>Thanks again!</p></div>ndechmantag:robotlegs.tenderapp.com,2009-10-18:Comment/112877892011-11-11T13:17:54Z2011-11-11T13:17:54ZInjection fails into non-singleton model<div><p>No problem!</p>
<p>And thanks for expressing your appreciation - I'm now warm and
fuzzy :)</p>
<p>If you run into problems implementing the solution you choose,
shout again and we'll try to figure it out with you in more
detail.</p>
<p>Sorry it took a little longer than normal to get you a fix as
well - we're slightly distracted by building Robotlegs 2 at the
moment, I'm glad you came back and asked again, as I had meant to
respond to your second question yesterday and it had slipped my
mind.</p>
<p>Cheers!</p>
<p>Stray</p></div>Stray