Dependency Injection; Ok but how?

More Robotlegs for you guys, but this time in is more conceptual.

One thing that bugged me with Robotlegs was the use of the expression Dependency Injection. Cool word, it must mean something huge. Well if you look at it quickly; not really. If you take time to think about it, it means more.

Half of it is injection

Well, how you implement dependency injection is actually pretty simple and is something that you are doing every day (well if you program). Dependency Injection is giving, by the mean of the contructor, a method or a property, dependency (data) to an object. As Joel Hooks said it in his InsideRIA article (read it, it’s a good intro): “When you pass a variable to the constructor of a class, you are using Dependency Injection. When you set a property on a class, you are using Dependency Injection.

Ain’t that just pleasing; I can just walk around the office and tell every one I am doing Dependency Injection.

The concept behind it

I first was reading about this on the Robotlegs best practices and I couldn’t understand anything (that’s mostly the case when I am first exposed to a design pattern, no offense to that document). After that I found Hooks article and I said to myself: “this ain’t complicated, why all the fuss”, but I wasn’t really understanding the concept (the why) behind it. It took me this article to really understand. The example is really simple and clearly expose why we should use dependency injection.

Why we should use dependency injection is mostly to create more flexible Classes. If a Class as settings that could change and that it depends on them to work, these settings should not be set inside the Class’ code but outside of it. That way every time the settings change, you don’t need to go in the Class’ code to change them.  You should really read Fabien Potentier’s article about it; he does a way better job at explaining this than me. Also this presentation by Jeff More is pretty good. The more you read about it the more you’ll understand what it is.

Fine but it still feels like magic in Robotlegs

When you read the wikipedia article on Dependency Injection, at one point they list some draw backs and one of them was that “Code that uses dependency injection can seem magical to some developers” and that is exactly how I felt about it in the context of Robotlegs. Mostly because of the use of the [Inject] metatag. That is not a mechanism I was used to in AS3. I was thinking that these meta where holy blessed keywords that only Adobe could create.

Well it turns out I was wrong. Well half wrong. The [Inject] meta is used at runtime while let’s say the [Embed] metatag is used at compile time, so it is not exactly the same beast. In Robotlegs, injection is handled by the SwiftSuspenders. What it does is that for all rules you create using the method mapValue, mapClass and mapSingleton it will inspect the classes it receive. It uses the function flash.utils.describeType on the Class to do so, this will return an XML that represent that Class. In this XML, there will be tags that represent the [Inject] metatag. That is what SwiftSuspenders is looking for when parsing the class representation XML, after that it can freely do the injection (passing the values) according to the rules.

Now you could go and create your own metatags, but it seems that the compiler would remove them at compilation. If you use the source for the SwiftSuspenders instead of the SWC they tell you to add this to the compiler arguments:


-keep-as3-metadata+=PostConstruct //This is another metatag that SwiftSuspenders makes use of

This will prevent the compiler from removing the metatags from the Classes, so you could basically change these lines to make the compiler keep your newly created metatags. I have no idea why you don’t have to do this when using the SWC.

That is kinda what I wanted to cover. I’m still not fully comfortable with dependency injection but at least I have a better idea of how it works underneath. I hope you feel the same.

, , , , , , , ,

  1. #1 by Jamie McDaniel - March 17th, 2010 at 09:34

    “I was thinking that these meta where holy blessed keywords that only Adobe could create.”

    Me too. I had not used Swiz or Robotlegs yet, just Cairngorm and Mate (which I like a lot except for the “magic strings”) and they both do not make use of metadata. So thanks for including in your post about how the [Inject] and [PostConstruct] metadata is used at runtime via flash.utils.describeType. Here is the documentation for that method (I believe it was the only thing that wasn’t linked in your post.)

  2. #2 by - March 17th, 2010 at 10:59

    Thks for the comment, I added the link in the page also.

  3. #3 by Jassa - March 17th, 2010 at 20:37

    Nice overview- the links were super helpful. Thanks!

  4. #4 by Alexandru Ghiura - March 19th, 2010 at 07:15

    About why you don’t need to use -keep-as3-metadata +=Injection if you use the SWC you can find the answere here:

  5. #5 by - March 19th, 2010 at 10:09

    @ Alexandru Ghiura
    Thx for the link, it make sense that if you use a library(SWC) that needs to keep a certain metatag, than that project using that library doesn’t have to add the keep-as3-metadata compiler argument

  6. #6 by saumya - June 6th, 2010 at 13:27

    Thanks for the explanation. Much needed. I am still struggling to get up and running in RobotLegs. So reading on a lot,what might have gone wrong.

  7. #7 by Enrique - October 30th, 2010 at 00:07

    THANKS! I’ve bookmarked your blog.

  8. #8 by Enrique - October 30th, 2010 at 00:25

    Also, here you can fllow 3 simple steps for custom metadata:

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.

Parse error: syntax error, unexpected ';' in /homepages/25/d169645162/htdocs/wp-content/themes/fusion/footer.php on line 13