Doug Bryant

Tech thoughts and notes

Dependency Injection: Vitally Important or Totally Irrelevant

by Jim Weirich

Slides at http://onestepback.org/articles/depinj

Is Dependency Injection vitally important in a dynamically typed language

  • Short version – no
  • Medium version – maybe
  • Long version – don’t know

Who are you? Perhaps not who you think you are.

Building a computer controlled coffee maker. When coffee in put – burner should be on. When pot not in or no coffee in put – burner should be off.

Talks about the problem of Concrete classes being tied together and show some solutions in java and ruby (ruby modeled off java code). Push problem out by using interfaces, constructor args, getter/setters, etc but problem is just moved from one place to another.

One solution – factory pattern. But… cumbersome and invasive (examples)
Another solution – Service Locators – invasive, order dependent (examples)

Goes through a typical dependency injection system in ruby: DIY module

Gains: Flexibility and Testability Losses: complexity and indirection

This makes sense in java, but what about ruby?

Based dependency injection example in ruby on how java classes work.

Java classes are hard

  • Not objects
  • (mostly) source code constructs
  • unchangable at runtime
  • Class name directly identifies class

Ruby Classes are soft

  • Just objects
  • Runtime live
  • changable at runtime

Is Dependency Injection relevant in dynamic languages – perhaps on very large projects, but the jury is still out.