tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/264-how-do-you-use-injectormapsingletonmapsingletonof-with-abstract-classes-interfacesRobotlegs: Discussion 2018-10-18T16:35:16Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/27201692010-08-27T09:17:36Z2010-08-27T09:17:37ZHow do you use injector.mapSingleton/mapSingletonOf with abstract classes & interfaces?<div><p>Hi there,</p>
<p>I'm looking at the best practices document which says:</p>
<p>"mapSingletonOf is much like mapSingleton in functionality. It is useful for mapping abstract classes and interfaces, where mapSingleton is for mapping concrete class implementations."</p>
<p>I've got 2 different services, each of which extend the same abstract class (AbstractFileLoaderService) and implement the same interface (IFileLoaderService). If I try and use mapSingletonOf like so:</p>
<p>injector.mapSingletonOf(IFileLoaderService, ConcreteServiceA);<br />
injector.mapSingletonOf(IFileLoaderService, ConcreteServiceB);</p>
<p>and then I inject those services into commands like this:</p>
<p>[Inject] public var concreteServiceAInstance:IFileLoaderService;</p>
<p>and</p>
<p>[Inject] public var concreteServiceBInstance:IFileLoaderService;</p>
<p>What ends up happening is the injector doesn't know which concrete service it's supposed to inject which it encounters the interface in the inject commands so it does the same one twice. While it makes sense to me <em>why</em> it does this, I'm not sure what the proper way to resolve this is. I can refactor it to use injector.mapSingleton and just inject the concrete classes and everything works, but doesn't this go against the whole point of mapSingletonOf and interfaces?</p></div>Davetag:robotlegs.tenderapp.com,2009-10-18:Comment/27201692010-08-27T09:28:38Z2010-08-27T09:28:41ZHow do you use injector.mapSingleton/mapSingletonOf with abstract classes & interfaces?<div><p>You can use named injection:</p>
<pre><code>injector.mapSingletonOf( IFileLoaderService, ConcreteServiceA, 'nameOfA' );
injector.mapSingletonOf( IFileLoaderService, ConcreteServiceB, 'nameOfB' );</code>
</pre>
<p>In your injected class retrieve the injection by [Inject name="nameOfA"].</p>
<p>Hope this solution solves your problem.</p></div>r.moormantag:robotlegs.tenderapp.com,2009-10-18:Comment/27201692010-08-27T09:43:27Z2010-08-27T09:43:30ZHow do you use injector.mapSingleton/mapSingletonOf with abstract classes & interfaces?<div><p>Perfect! Thanks for the fast reply.</p></div>Dave