Inject singleton in static (random) class

ben's Avatar


24 Jul, 2013 09:31 AM

Hi, I have a hunch this question has been asked a million times already, but because I don't have a full comprehension of the inner workings yet, I can't make much out of them. So hope somebody can easily see which thinking-error I'm making.

The problem:
I want to use a mapped singleton (in Context) in a random (non-framework) class. That class is static. The Injected variables are null there.

The would-like-it-to-work:

public class Whatever
    public static var singletonInstance:MySingleton;

The Context (in startup function):

injector.mapSingleton( MySingleton );

I read that there is some magic I need to do, before the injection will actually work. But I can't figure out which one exactly. I tried using mapValue instead of mapSingleton, but that has no effect. Is it even possible to use this kind of injection in a static class?



  1. Support Staff 1 Posted by creynders on 24 Jul, 2013 09:45 AM

    creynders's Avatar

    There's hardly ever a reason to store an instance in a static member. Only
    very, very, _very_ specific situations warrant it.
    The whole point of a DI-framework is to soothe the pain of passing
    instances around or needing some kind of service locator pattern.
    What exactly is it you want to do?

    TBH I'm not sure if it's possible to inject into static members, I suppose
    it is, but I've never tested it out (and I've been using RL for something
    like 3 or 4 years now)

  2. 2 Posted by ben on 24 Jul, 2013 09:51 AM

    ben's Avatar

    Thanks for the quick reply. The "Whatever" class above, is a very low-tech utility class that converts some units for me. For the right conversion, it needs access to one value which is stored in the Model (and which changes in the lifetime of the app).

    There is no bigger plan as to why it is a static, besides it being the easiest (for me) to use elsewhere (as in, shortest syntax). So any tips on a better approach are more than welcome.

    Would you rather use a Singleton for it? If that will allow for injection, instead of static, I have no problem converting it to one.

  3. Support Staff 3 Posted by creynders on 24 Jul, 2013 10:05 AM

    creynders's Avatar

    Whether utility classes should or should not be static classes is up for debate, but I also use static classes for utilities, but on one very strict condition: they don't store state in any way (constants should not be counted as state)
    I'd let a command do the work, including the retrieval of the value from the model and pass the value to the utility class.

    Something like this:

    public var xModel: XModel;
    public function execute():void{
        const y : YUnit = UtilityClass.convertXToY(xModel.x);
        //do what you want with y
  4. 4 Posted by neil on 24 Jul, 2013 10:26 AM

    neil's Avatar

    Don't store the model in a util class,
    Util classes are pure methods (only working on inputs from arguments)
    I would create a class instance that injects your model and uses the util...

    I see that creynders has already answered with using a command, which would be the most obvious application

  5. 5 Posted by ben on 24 Jul, 2013 10:58 AM

    ben's Avatar

    Thanks again guys, I'll go with the approach of passing the needed model references through the arguments of the functions. Kudos for the fast and helpfull responses.

    Just to help me getting my head around it. What would be the "magic" call I needed tot make the injection work in normal non-framework, non-static classes? Would I need to tell the injector something special?


  6. Support Staff 6 Posted by Shaun Smith on 24 Jul, 2013 08:52 PM

    Shaun Smith's Avatar

    What would be the "magic" call I needed tot make the injection work in normal non-framework, non-static classes? Would I need to tell the injector something special?

    Yup, you'd need to either:

    1. Use the injector to create the instance:

      instance = injector.instantiateUnmapped(ClassWithInjectionPoints);

    2. Inject into an existing instance:


    The Injector is really just a factory, so it can't know anything about instances that it doesn't create or process.

  7. 7 Posted by ben on 24 Jul, 2013 09:07 PM

    ben's Avatar

    Thanks! Those were the ones I thought i should use. Just couldn't put the pieces together in my head.


    On 24 jul. 2013, at 22:52, Shaun Smith <[email blocked]> wrote:

  8. Ondina D.F. closed this discussion on 03 Aug, 2013 01:36 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