Monday, August 06, 2007

Dependency Injection in One Sentence

Dependency Injection enables a vitally important but nevertheless weak, limited, syntactically confounding, and dauntingly complex form of one of the kinds of meta-programming that should exist in the language.

4 comments:

Jim's original post mentions explaining why DI is necessary in Java and rarely so in Ruby.

Your offering clearly states problems with DI but should one infer that you feel Ruby solves those issues? i.e. that Ruby does in fact have strong meta-programming, clarity, etc etc. (If so, that's fine, it's just a bit ambiguous).

Your assumption is correct (and I couldn't easily get it crammed into a single sentence). Dependency injection-like behavior is trivial in Ruby and similar strong meta-programming languages. Just to clarify, it's not a fault of Dependency Injection: Java would be severely compromised without it. It's more about needing a mechanism like that in the Java language, which you don't need in more dynamic languages. Which is one of the reasons I think we're seeing a resurgence of interest in dynamic languages.

What I read into this is that meta-programming facilities for defining dependencies are built-in to Ruby, but provided by add-on libraries in Java. Correct? What I'm not clear on is how this idea is general to dynamic languages.

This feature is not necessarily an intrinsic part of dynamic languages, but the interesting mainstream ones have strong enough meta-programming support that what we call "dependency injection" in Java is trivial in those languages.