What sorts of dependencies are candidates for injection?

gmastey's Avatar


25 May, 2013 01:41 AM

Hi there,

I'm a fairly experienced AS3 developer, but new to Robotlegs. I've got a handle on the major concepts, read the O'Reilly book, and have dug into a few demo projects. I'm stoked. But one thing I can't quite wrap my head around is when to use dependency injection.

I get the argument against statics, and I can clearly see the benefit of injecting custom classes. But what about base classes like Strings, int, Number, Boolean, Array, and others? What is the benefit of injecting these? If I need a Boolean to hold some bit of state in a class, why use injection for such a minor thing?

I've poured over a number of demo projects, including the Mosaic & Kanban apps discussed in the O'Reilly book, and dependencies are never injected for base classes in any of them. Instead they are declared as protected vars. However, in the book itself there is some mention of injecting Strings and especially Arrays.

To confuse things further, flash components like Labels and TextFields are also not injected in the demo projects I've looked at.

So what I'm asking is: What are the best practices for dependency injection, and what sorts of dependencies are candidates for injection?

If you got this far, thank you!


  1. 1 Posted by Paul Robertson on 25 May, 2013 05:13 AM

    Paul Robertson's Avatar

    The key benefit of dependency injection is that it creates a "seam."
    Because it provides a mechanism for getting access to an object without
    having to define how the object is created, you can easily substitute some
    other object in its place (most commonly for testing purposes, but also to
    give you flexibility to easily change how you implement a particular part
    of your app).

    So a common candidate for dependency injection would be something that a
    class needs in order to do its job, that you might want to replace with a
    "mock" version in a testing use case.

    Usually you're doing injection in a model (so you can easily test your
    business logic) or in a service (so you can test how the service calls and
    responds). In addition, utilities such as data adapters that you would
    likely want to test.

    In addition, in Robotlegs you use injection in commands and mediators. This
    isn't so much so you can test them (since they're ideally more just message
    passers rather than doing any important calculations). Rather its for
    convenience to keep the parts of your app separated from each other to keep
    them from being tightly connected.

    Consequently, there's rarely any need to inject a primitive value such as a
    string or number. They just don't match the common use cases. (You might
    have a string such as a service URL that is a dependency, but it's
    preferable to make it a property of a model object and either inject the
    model or pass the string in through a call to the service method.) One big
    reason for tis is because those types are just too common, so it's not as
    straightforward to inject them. You end up using a "magic string" such as
    an id or name, but that's not preferred because its subject to typos and
    being changed in one place but not another.

    The reason display objects such as UI components aren't injected is because
    then the application becomes too tightly coupled. It's best to just let the
    parent view (e.g. A Sprite) be the only one that knows about the
    particulars of the UI, since that's an area that could change frequently.
    The mediator can just call methods on the view and it can handle the
    management of the components.

    Hopefully that helps. Feel free to ask for ore clarification. I'll try to
    give more concrete examples (those aren't as easy to type on a
    mobile device).


  2. 2 Posted by gmastey on 27 May, 2013 04:54 PM

    gmastey's Avatar

    Hi Paul,

    Thanks so much for this explanation. I definitely see the benefits for testing, and for avoiding tight coupling. It seems like Robotlegs is a clever way to enforce good OOP. I'm relieved that I don't need to inject primitive values - that would be so incredibly tedious.



  3. gmastey closed this discussion on 28 May, 2013 02:12 AM.

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