tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2421-inject-singleton-in-static-random-classRobotlegs: Discussion 2018-10-18T16:35:50Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T09:45:45Z2013-07-24T09:45:45ZInject singleton in static (random) class<div><p>There's hardly ever a reason to store an instance in a static
member. Only<br>
very, very, <em>very</em> specific situations warrant it.<br>
The whole point of a DI-framework is to soothe the pain of
passing<br>
instances around or needing some kind of service locator
pattern.<br>
What exactly is it you want to do?</p>
<p>TBH I'm not sure if it's possible to inject into static members,
I suppose<br>
it is, but I've never tested it out (and I've been using RL for
something<br>
like 3 or 4 years now)</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T09:51:20Z2013-07-24T09:51:20ZInject singleton in static (random) class<div><p>Thanks for the quick reply. The "Whatever" class above, is a
very low-tech utility class that converts some units for me. For
the right conversion, it needs access to one value which is stored
in the Model (and which changes in the lifetime of the app).</p>
<p>There is no bigger plan as to why it is a static, besides it
being the easiest (for me) to use elsewhere (as in, shortest
syntax). So any tips on a better approach are more than
welcome.</p>
<p>Would you rather use a Singleton for it? If that will allow for
injection, instead of static, I have no problem converting it to
one.</p></div>bentag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T10:05:39Z2013-07-24T10:05:39ZInject singleton in static (random) class<div><p>Whether utility classes should or should not be static classes
is up for debate, but I also use static classes for utilities, but
on one very strict condition: they don't store state in any way
(constants should not be counted as state)<br>
I'd let a command do the work, including the retrieval of the value
from the model and pass the value to the utility class.</p>
<p>Something like this:</p>
<pre>
<code>//ConvertXToYCommand
[Inject]
public var xModel: XModel;
public function execute():void{
const y : YUnit = UtilityClass.convertXToY(xModel.x);
//do what you want with y
}</code>
</pre></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T10:26:30Z2013-07-24T10:26:30ZInject singleton in static (random) class<div><p>Don't store the model in a util class,<br>
Util classes are pure methods (only working on inputs from
arguments)<br>
I would create a class instance that injects your model and uses
the util...</p>
<p>I see that creynders has already answered with using a command,
which would be the most obvious application</p></div>neiltag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T10:58:40Z2013-07-24T10:58:40ZInject singleton in static (random) class<div><p>Thanks again guys, I'll go with the approach of passing the
needed model references through the arguments of the functions.
Kudos for the fast and helpfull responses.</p>
<p>Just to help me getting my head around it. What would be the
"magic" call I needed tot make the injection work in normal
non-framework, non-static classes? Would I need to tell the
injector something special?</p>
<p>Cheers!</p></div>bentag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T20:52:34Z2013-07-24T20:52:34ZInject singleton in static (random) class<div><blockquote>
<p>What would be the "magic" call I needed tot make the injection
work in normal non-framework, non-static classes? Would I need to
tell the injector something special?</p>
</blockquote>
<p>Yup, you'd need to either:</p>
<ol>
<li>
<p>Use the injector to create the instance:</p>
<p>instance =
injector.instantiateUnmapped(ClassWithInjectionPoints);</p>
</li>
<li>
<p>Inject <code>into</code> an existing instance:</p>
<p>injector.injectInto(instance);</p>
</li>
</ol>
<p>The Injector is really just a factory, so it can't know anything
about instances that it doesn't create or process.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/279302922013-07-24T21:07:12Z2013-07-24T21:07:12ZInject singleton in static (random) class<div><p>Thanks! Those were the ones I thought i should use. Just
couldn't put the pieces together in my head.</p>
<p>Cheers.</p>
<p>On 24 jul. 2013, at 22:52, Shaun Smith <a href=
"mailto:tender+d9c02403e2e25909869c34cc60766b88154a2367f@tenderapp.com">
tender+d9c02403e2e25909869c34cc60766b88154a2367f@tenderapp.com</a>
wrote:</p></div>ben