Trying to understand dependency injection

Coming closer to the end of writing my first really complex app, I’m beginning to wish I’d taken the time at the start to properly learn and grasp some of the concepts that I see other programmers using.  My client-side code uses a basic MVC pattern, but a bunch of Flash Developers use something that to me comes across as much more complex – as seen in RobotLegs or some of the Haxe ports, such as MMVC.

These frameworks are all about Inversion Of Control (and because we love acronyms: IOC).  And they use Dependency Injection (DI) to do this.  You can see already why the learning curve can be steep, especially if you haven’t come across this pattern before.

So today I googled “I don’t understand dependency injection” and got this fantastic explanation by Kevin William Pang.  I found that really helpful, and I’m sold.  I’d been wondering how the hell I was supposed to unit-test some of my more complex client code anyway… If you’re one that, like me, hasn’t understood this concept in much detail: his article is well worth a read, I won’t bother to try and explain it here.

But, I’m still not sure where the fancy dependency injection tools and frameworks come in.  I see the value in it, and I think “injecting” it into the constructor by using arguments given to the constructor seems just fine.  Meanwhile, the tools which are supposed to help, such as SwiftSuspenders or Minject seem to me to be really verbose, and not offer a lot more than the constructor method.

Maybe that style is more relevant for gaming.  Or maybe it’s benefits show up with more complex projects.  Or maybe I just haven’t been enlightened.

Either way, I’ll try to get enlightened… I think it’s worth spending time learning these patterns.  They’ve obviously gained traction for a reason, and a lot of better developers than me swear by them, so I’ll try wrap my head around it.  Maybe one day it’ll seem as clear to me as MVC, or even OOP do now (both of which took me a while at first).

If you have any suggestions / comments / explanations – I’d love to hear them in the comments section.

2 thoughts on “Trying to understand dependency injection

  1. Hello again!

    This talk of Misko Hevery was an DI eye opener for me:
    http://www.youtube.com/watch?v=RlfLCWKxHJ0&feature=player_embedded#at=86

    Also this article from Shaun Smith, creator of RobotLegs AS3, about constructor versus setter injection.

    I’m no expert at all, but I find it pretty cool to be able to – whenever I for example need a service instance in a mvc controller – just add the interface name of it as a controller constructor parameter, and the DI setup takes care of providing the correct instance of it, depending on current configuration. (This is the way it works in a current c# .net mvc project that I’m taking part in.)

    With the Haxe macro power, I guess that it would be possible to create a very easy to use DI solution…

    J

Leave a Reply

Your email address will not be published. Required fields are marked *