Skip to main content
Participant
November 12, 2010
Question

Dependency Injection

  • November 12, 2010
  • 1 reply
  • 594 views

Hello,

I would like to be able to create an auto dependency injection system in flash. I am not completely sure what is the best way to handle this in flash. My first thought is having custom meta tags:

[Singleton]

class TheSingleton()

{

     public DoSomething() {}

}

class TheNewObjec()

{

     public DoSomething() {}

}

class MyObject()

{

    [Inject]

     public MyObject(TheSingleton theSingleton, TheNewObject theObject )

     {

          theSingleton.DoSomething();

          theObject .DoSomething();

     }

}

Instanciate(obj:*)

{

     dependencies = obj.getDependencies();

     for each ( dep in dependencies )

     {

          if ( dep.metaTag("Singleton") == true )

          {

               return _singletons[dep];

          }

          else

          {

               return new dep;

          }
      }

}

}

MyObject obj = Instanciate(MyObject);

Does this seem like the best approach?

Thanks

This topic has been closed for replies.

1 reply

Participating Frequently
November 13, 2010

I'd recommend using Swiz, which already implements this exact

approach: http://swizframework.org/

-- Daniel R. <danielr@neophi.com>