tag:robotlegs.tenderapp.com,2009-10-18:/discussions/suggestions/56-mapsingletonmyclass-and-mapsingletonofimyclass-myclass-to-provide-single-instance-of-myclassRobotlegs: Discussion 2018-10-18T16:35:27Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/73053462011-05-18T11:35:49Z2011-05-18T11:35:50ZmapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass<div><p><code>injector.mapSingleton(MyClass);
injector.mapSingletonOf(IMyClass, MyClass);</code></p>
<p>... results in having 2 instances of MyClass. While I personally
would expect 1.</p>
<p>Why would I ever write this code? Well injection of IMyClass is
done in some application-non-specific view/mediator. And injection
of MyClass is done in a command, that is really
application-specific and requires MyClass instead of IMyClass
(because of additional API).</p>
<p>Current workaround: use mapValue:<br>
var myClassInstance:MyClass = new MyClass();<br>
injector.mapValue(MyClass, myClassInstance);<br>
injector.mapValue(IMyClass, myClassInstance);<br>
which is not very elegant, though possible.</p></div>pavel.fljottag:robotlegs.tenderapp.com,2009-10-18:Comment/73053462011-05-18T11:48:41Z2011-05-18T11:48:41ZmapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass<div><p>Hi Pavel - why do you think it's not elegant? It looked ok to me
:)</p>
<p>an alternative (if MyClass itself has injections) would be:</p>
<p>injector.mapSingleton(MyClass);<br>
injector.mapValue(MyClass, injector.getInstance(MyClass));<br>
injector.mapValue(IMyClass, injector.getInstance(MyClass));</p>
<p>but I think either is fine.</p>
<p>I see what you mean though - it's a little confusing that it
produces 2 instances. I'm not sure whether this is intended or
incidental - we'll look at use-cases like yours for version 2 and
work out what the least confusing approach is. It might well be
that we can do something more obvious <em>and</em> more flexible
with Till's lovely new fluent API for SwiftSuspenders.</p>
<p>Thanks for brining this up,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/73053462011-05-18T11:56:10Z2011-05-18T11:56:10ZmapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass<div><p>Thanks for alt way, this one if more bullet-proof (luckely there
was not injections in MyClass, so I haven't discovered new possible
problem).</p>
<p>Hope u marked it and won't forget!..</p></div>pavel.fljottag:robotlegs.tenderapp.com,2009-10-18:Comment/73053462011-05-18T12:33:10Z2011-05-18T12:33:10ZmapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass<div><p>Actually, this behavior is intentional: It allows for reusing
the same<br>
singleton model multiple times for multiple different use-cases. To
be<br>
quite honest, I'm not too sure that it's really that important,<br>
though. I am, on the other hand, sure that it wouldn't be that easy
to<br>
change without impeding the injector's performance: Lookup is
always<br>
done by request-type, but in this case, it would have to be done
by<br>
response instead.</p>
<p>Also note that there's yet another, slightly simpler, way to
solve your problem:</p>
<p>var rule : * = injector.mapSingleton(MyClass);<br>
injector.mapRule(IMyClass, rule);</p>
<p>here's the relevant line in the injector:<br>
<a href=
"https://github.com/tschneidereit/SwiftSuspenders/blob/master/src/org/swiftsuspenders/Injector.as#L90">
https://github.com/tschneidereit/SwiftSuspenders/blob/master/src/or...</a></p>
<p>(Why am I using "*" instead of a proper type, you ask? Because
AS3 doesn't provide static duck typing, that's why! Oh, and also
because<br>
we didn't want to break Robotlegs' ability to switch out
injector<br>
implementations, so IInjector#mapRule can't take a type as a
parameter<br>
that only exists in Swiftsuspenders.)</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/73053462011-05-18T12:41:14Z2011-05-18T12:41:14ZmapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass<div><p>@tschneidereit</p>
<p>thank u very much, looks just elegant and works as expected! And
your explanation finally made some sense in my head now.</p></div>pavel.fljot