Automatic Dependency Injection In The Land Of Dynamic Languages

October 25th, 2009 · 5 Comments ·

Automatic Dependency Injection Frameworks (such as GUICE, PicoContainer, and Spring) are very popular in Java, where they depend heavily on static typing, interfaces, reflection and annotations. But what about languages such as ActionScript, JavaScript, Python and Ruby, where duck-typing and lack of run-time-type-information at first glance make these frameworks impossible? Can a simple Dependency Injection Framework framework be built? We’ll look at how the power of functional programing and functions as first class citizens in these languages can be leveraged to build a simple and effective dependency injection framework. We’ll also explore the power of using such a framework in dynamic languages. To illustrate these ideas we will convert a JavaScript application which uses manual Dependency Injection into one using Automatic Dependency Injection Framework.

The framework implementation can be found here: http://bitbucket.org/misko/misko-hevery-oopsla-09/overview/

The paper can be found here: http://misko.hevery.com/wp-content/uploads/2009/10/tut0000021-hevery.pdf

To get the injection code make sure to switch branches to “inject”

Tags: Uncategorized

5 responses so far ↓

  • MySchizoBuddy // Oct 28, 2009 at 2:43 pm

    Is their DI for Objective-C

  • Rafael de F. Ferreira // Oct 29, 2009 at 1:14 pm

    Is there a paper to accompany the code?

  • misko // Oct 29, 2009 at 2:39 pm

    yes, i have updated the site

  • gropapa // Jan 7, 2010 at 2:51 am

    hi Misko, i love your articles but sometimes, your advices cannot be implemented.
    i d like to ask you a question about testability and DI,
    To keep it simple, imagine a swf in charge of communicationg with a server, in the code we have like a concrete socket conected to a real server.
    this module will be loaded and used as a component.
    Now, since it is loaded, the constructor is automaticaly executed, there is no way i can pass a mock socket, so in this case, is the use of a public init() method acceptable?
    How can i build a mockModule in order to test other pieces of the application?

  • Jasper Blues // Jan 13, 2013 at 10:11 pm

    @MySchizoBuddy

    There’s lots of DI options for Objective-C these days.

    Two weeks ago, I created one called: https://github.com/jasperblues/spring-objective-c