When to use [Inject(name="uniqueIdentifier")]?

prettymuchbryce's Avatar

prettymuchbryce

22 Sep, 2011 05:06 PM via web

Hello. I am new to robot legs and this might be a dumb question. I am confused by when I need to use the [Inject(name="uniqueIdentifier")] metadata tag? Is this only when I map two or more dependencies to inject? Or does this also include mapping views as well? I notice some examples like helloflash use two [inject] tags in the classes without the (name="uniqueIdentifier") tag.

  1. 2 Posted by Stray on 22 Sep, 2011 05:15 PM

    Stray's Avatar

    Hi there,

    named injection is only needed when it's otherwise ambiguous what to inject - so, only where you have 2 injection points of exactly the same type.

    So - this is fine:

    [Inject]
    public var itemA:ITypeA;

    [Inject]
    public var itemB:ITypeB;

    but this would need names:

    [Inject (name = "master")]
    public var itemA1:ITypeA;

    Inject (name = "local")]
    public var itemA2:ITypeA;

    So - the key thing is the *type* of the injection point. The injector always has to know exactly what to do with an injection point (the injector is just a factory that can inspect injection points to get a clue about what to do). The names clear up any ambiguity where otherwise the injector might not know what to inject (or injections would be overwritten).

    But... named injection is pretty weak as a solution - generally it's better to use strong typing instead.

    Stray

  2. Support Staff 3 Posted by Joel Hooks on 22 Sep, 2011 05:18 PM

    Joel Hooks's Avatar

    I have never used named injection in production.

  3. 4 Posted by prettymuchbryce on 22 Sep, 2011 05:24 PM

    prettymuchbryce's Avatar

    Thank you Stray and Joel. That clears up my misunderstanding.

  4. Ondina D.F. closed this discussion on 01 Nov, 2011 02:10 PM.

Comments are currently closed for this discussion. You can start a new one.