mapSingleton(MyClass) and mapSingletonOf(IMyClass, MyClass) to provide single instance of MyClass

pavel.fljot's Avatar


18 May, 2011 11:35 AM

injector.mapSingleton(MyClass); injector.mapSingletonOf(IMyClass, MyClass);

... results in having 2 instances of MyClass. While I personally would expect 1.

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).

Current workaround: use mapValue:
var myClassInstance:MyClass = new MyClass();
injector.mapValue(MyClass, myClassInstance);
injector.mapValue(IMyClass, myClassInstance);
which is not very elegant, though possible.

  1. 1 Posted by Stray on 18 May, 2011 11:48 AM

    Stray's Avatar

    Hi Pavel - why do you think it's not elegant? It looked ok to me :)

    an alternative (if MyClass itself has injections) would be:

    injector.mapValue(MyClass, injector.getInstance(MyClass));
    injector.mapValue(IMyClass, injector.getInstance(MyClass));

    but I think either is fine.

    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 *and* more flexible with Till's lovely new fluent API for SwiftSuspenders.

    Thanks for brining this up,


  2. 2 Posted by pavel.fljot on 18 May, 2011 11:56 AM

    pavel.fljot's Avatar

    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).

    Hope u marked it and won't forget!..

  3. Support Staff 3 Posted by Till Schneidere... on 18 May, 2011 12:33 PM

    Till Schneidereit's Avatar

    Actually, this behavior is intentional: It allows for reusing the same
    singleton model multiple times for multiple different use-cases. To be
    quite honest, I'm not too sure that it's really that important,
    though. I am, on the other hand, sure that it wouldn't be that easy to
    change without impeding the injector's performance: Lookup is always
    done by request-type, but in this case, it would have to be done by
    response instead.

    Also note that there's yet another, slightly simpler, way to solve your problem:

    var rule : * = injector.mapSingleton(MyClass);
    injector.mapRule(IMyClass, rule);

    here's the relevant line in the injector:

    (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
    we didn't want to break Robotlegs' ability to switch out injector
    implementations, so IInjector#mapRule can't take a type as a parameter
    that only exists in Swiftsuspenders.)

  4. 4 Posted by pavel.fljot on 18 May, 2011 12:41 PM

    pavel.fljot's Avatar


    thank u very much, looks just elegant and works as expected! And your explanation finally made some sense in my head now.

  5. pavel.fljot closed this discussion on 18 May, 2011 12:41 PM.

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

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac