tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/970-how-to-use-a-constant-as-the-name-of-a-named-injectionRobotlegs: Discussion 2012-08-25T14:42:40Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/175690212012-07-25T06:39:08Z2012-07-25T06:39:08ZHow to use a constant as the name of a named injection<div><p>Hi Chris,</p>
<p>For named injections you have to use the 3rd parameter,
„named“:</p>
<p>mapSingletonOf(whenAskedFor:Class, useSingletonOf:Class,
named:String = null)</p>
<p>The correct mapping:<br>
injector.mapSingletonOf(IDummyService, DummyService,
<strong>"myDummy"</strong>);</p>
<p>Named injection:</p>
<p>[Inject <strong>(name="myDummy")</strong>]</p>
<p>public var service:IDummyService</p>
<p>or</p>
<p>mapSingleton(whenAskedFor:Class, named:String="");</p>
<p>injector.mapSingleton(DummyService,
<strong>"myDummy"</strong>);</p>
<p>[Inject <strong>(name="myDummy")</strong>]</p>
<p>public var service:DummyService</p>
<p>HTH<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/175690212012-07-25T06:53:52Z2012-07-25T06:53:52ZHow to use a constant as the name of a named injection<div><p>Sorry, but I should have made it clearer that in my example
MyClassConstantName is defined as:-</p>
<pre>
<code>static public constant MyClassConstanName:String = "myClassConstantName";</code>
</pre></div>Christag:robotlegs.tenderapp.com,2009-10-18:Comment/175690212012-07-25T07:06:12Z2012-07-25T07:06:12ZHow to use a constant as the name of a named injection<div><p>Hi Chris,</p>
<p>unfortunately you can't. Metadata can't take in a constant from
the class. The metadata pre-exists the class, so at the point where
the metadata is created, it is too early to read your constant.</p>
<p>(Till would be able to tell you this with more precision, but
I'm guessing the bit you care about is that it doesn't work).</p>
<p>So you'll need to use the string literal.</p>
<p>This is one of the reasons why some of us really don't like
named injection. Others find it useful - I suspect they are better
at being accurate than me!</p>
<p>Stray</p></div>Stray