

Poor man's dependency injection in Clojure - pyritschard
http://spootnik.org/entries/2014/01/25_poor-mans-dependency-injection-in-clojure.html

======
eigenrick
This is just my opinion, but if one is writing dependecy-injected frameworks
in Clojure, then they're missing the point of Clojure.

~~~
chriswarbo
That's exactly what I was thinking. I've not used Clojure specifically, but in
the sitations where OO people use dependency injection I tend to use a free
monad with multiple interpreters. I'm yet to make serious use of an algebraic
effect system, but that would make this even simpler and more composable.

~~~
pacala
Could you please share a few examples?

------
Qerub
The one-function protocols strike me as unidiomatic; why not use functions
directly? `(fn […] …)` would work just as well as `(reify P (f [this …] …))`
(`this` is never used).

Update: I looked at the code again and noticed that `stdin-transport`
implements _two_ protocols. Guess I need to think a little bit more about
this.

------
moomin
Check out Stuart Sierra's "component" for another take. Of course, another
take is that you're better off just constructing objects by calling their
constructors directly.

~~~
pyritschard
Agreed, there are much more elaborate approaches than this one.

------
gengstrand
A chapter from the O'Reilly book covers this subject pretty well. With
Clojure's java interop, you can still use Spring. Thanks to the homoiconicity
of the language, support for higher order functions, and with some clever use
of Clojure's load-file function in core, you kind of get DI baked into the
language for free.

