Inject into a static factory class?

Michael Wills's Avatar

Michael Wills

21 Mar, 2010 07:26 AM via web

Just wondering if it is possible to inject into a factory class for use in a static method. Tonight I had a case where I wanted to use a function in my model in a factory but it was only needed in one place at one time so it didn't seem like it warranted pulling it out into a static function itself or redefining it as a static function in the model. So I was hoping I could inject into the factory class. The result was a null reference error so if it is possible I am just probably missing something very simple. The factory extended Actor (using mvcs) in hopes of being able to do that. I'll look into it again later though I was able to work around it. I'm sure I'm just missing something so any pointers would be most welcome.

And wow this framework rocks.

  1. 2 Posted by Jonny Reeves on 23 Mar, 2010 11:06 AM

    Jonny Reeves's Avatar

    Hi Michael,

    Could you possible paste the Static Factory class in question so we can have a look at it? My initial guess would be "no", however it does beg the question as to why the Factory needs to be static in the first place?

    Jonny.

  2. 3 Posted by Michael Wills on 23 Mar, 2010 06:53 PM

    Michael Wills's Avatar

    Ah the class itself isn't static, just the method being called. Unfortunately I can't post the code but it's pretty straightforward similar to this

    public class ItemFactory extends Actor
    {
    
        public static function makeItem( xml:XML ):ItemVO
        {
            var vo:ItemVO;
            if( xml != null )
            {
                vo = new ItemVO();
                vo.text = xml.text.toString();
                vo.name = xml.name.toString();
                vo.image1 = xml.image[0]***@src.toString();
                vo.caption1 = xml.image[0].caption.toString();
                vo.thumb = ***@src.toString();
            }
            return vo;
        }
    }
    

    There is a function in the model which can set the color which would require injecting a model instance accessible from that static method.

    Thinking about it now though, that factory class was never instantiated and as such there was never a chance to inject the variable. So if it weren't a static method and the factory was injected as a singleton instead, that would solve the problem. I was just used to thinking of Factory classes exposing static methods which is where the hangup was.

  3. 4 Posted by Jonny Reeves on 24 Mar, 2010 10:25 AM

    Jonny Reeves's Avatar

    Yep; I used to be the same and make all my Factories static; it's quite liberating to change this point of view ;)

  4. Stray closed this discussion on 11 Feb, 2011 11:36 PM.

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