Translation of the article by Dominic Gelino, on the topic of Dependency Injection and how it is implemented in the framework of Robotlegs. Dominic makes an attempt to dispel the sense of magic that a developer has when he uses Robotlegs injections.
Source:
www.zedia.net/2010/dependency-injection-ok-but-how
More Robotlegs for you guys, but this time in a more conceptual way.
')
One thought that bothered me in Robotlegs is the very expression Dependency Injection. A cool word, it must mean something significant. Yes it is, if you just looked at it; without going deep. If you take the time to think about it, you will understand that everything is a little different.
Half of what we do is Injection
Well, how you implement dependency injection is actually quite simple and that is what you do every day (of course, if you are programming). Dependency injection passes a dependency (data) to an object through a constructor, method, or property.
Joel Hooks wrote in an article on InsideRIA (read, good introductory article): “When you pass a variable to a class constructor, you use a dependency injection. When you set the value of a property in a class, you use a dependency injection ...
Isn't that nice; I can just walk around the office and tell everyone that I am doing dependency injections.
Concept what is the essence
At first I read about this in the best practices of Robotlegs and I could not understand anything (this is mainly because I first encountered this design pattern, and of course I have no complaints about this document). After I found
Hooks' article and I said to myself: “this is not so difficult, why I fussed so much,” but I didn’t have a real understanding of the concept (why) behind all this. In order to understand, I read
this article . The example is actually very simple and clearly clarifies why dependency injections should be used.
Why we need to use dependency injections is important mainly to create more flexible classes. If a Class uses a certain set of settings parameters that can change, and its operation depends on these parameters, then they should be set not in the Class code, but outside of it. Thus, every time you change the settings, you do not have to go into the Class code to change them. You really need to read
Fabien Poantier 's
article on this; he explains better than me. The
presentation made by Jeff More is also pretty good. The more you read, the more you will understand how it works.
Fine, but it still looks like the magic of Robotlegs.
When you read a Wikipedia article about dependency injection, in one place they give a list of some flaws and one of them was this - “Code using dependency injection can seem like magic to some developers,” and this is exactly what I felt in the context of Robotlegs. Mainly due to the use of the [Inject] meta tag. This is not the mechanism I'm used to using in AS3. I thought that Meta Tags are such holy blessed keywords that only Adobe can create.
It turns out I was wrong, well, well, half wrong. The [Inject] meta tag is used at runtime, while let's say the [Embed] meta tag is used at compile time, so this is not the same beast. In Robotlegs, the injection processes SwiftSuspenders. What this means is that for all the rules you create using the mapValue, mapClass, and mapSingleton methods, it looks at which classes should be returned. To do this, use the flash.utils.describeType function, which is defined in the class (Class), it returns the XML representing this class. The XML contains tags representing the [Inject] meta tag. It is their SwiftSuspenders that searches in the class view when parsing XML, after which it can freely inject (insert the desired value) according to the described rules.
Now you can of course go and create your own meta tags, but as it turned out, the compiler simply deletes them when compiling. If instead of SWC you use source codes for SwiftSuspenders, then you need to add to the compiler parameters:
-keep-as3-metadata+=Inject -keep-as3-metadata+=PostConstruct // - , SwiftSuspenders
This will prevent the compiler from removing meta tags from classes, so by changing these lines you will force the compiler to keep the meta tags you created earlier. I have no idea why not to do this when using SWC.
This is what I was able to cover. I still can’t say that I have fully understood addiction injections, but at least I have some understanding of how this works. I hope you feel the same ...