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”
5 responses so far ↓
Is their DI for Objective-C
Is there a paper to accompany the code?
yes, i have updated the site
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?
@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