Monday, August 06, 2007

Dependency Injection in One Sentence

Jim Weirich issued a challenge to describe dependency injection in once sentence. My friends at Relevance had a good version. Here's mine:


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.


This is fun -- can we do annotations(C#)/attributes(Java) next?

4 comments:

Michael Easter said...

Neal,

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).

Mike

Neal Ford said...

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.

Unknown said...

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.

Neal Ford said...

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.