Dead-Simple Dependency Injection

InfoQ · 28K views · 2012 · 27:04

Open on YouTube

Functional programmingSoftware architecture & designProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it as the standard introduction to doing dependency injection in Scala by passing arguments and using the Reader monad rather than a framework.

Recommended by 8 people across 6 different months, 2009 to 2020.

What it is

A 27-minute conference talk by Rúnar Óli Bjarnason (Capital IQ, Scalaz contributor) at the 2012 Northeast Scala Symposium. It argues that dependency injection in Scala can be done with plain function arguments, currying and the Reader monad instead of a DI framework.

What people said

Strong rec CrazyPyro · 2014-08 · Stack Overflow · on “Dependency Injection as a Language Feature?”

+1 For Scala. Check out the AWESOME talk, Dead-Simple Dependency Injection [this video] Runar is super-smart, and also hilarious: "Inversion of Control, which is really just a pretentious way of saying 'taking an argument'"

Recommends mergeconflict · 2012-06 · Stack Overflow · on “Configuration data in Scala -- should I use the Reader monad?”

Let's start with a simple, superficial difference between your approach and the Reader approach, which is that you no longer need to hang onto config anywhere at all. Let's say you define the following vaguely clever type synonym: type Configured[A] = ConfigSource => A Now, if I ever need a ConfigSource for some function, say a function that gets the n 'th client in the list, I can declare that function as "configured": def nthClient(n: Int): Configured[Client] = { config => config.clients(n) } So we're essentially pulling a config out of thin air, any time we need one! Smells like dependency injection, right? Now let's say we want the ages of the first, second and third clients in the list

Recommends Manuel Schmidt · 2012-06 · Stack Overflow · on “Using Reader Monad for Dependency Injection”

I recently saw the talks Dead-Simple Dependency Injection [this video] and Dependency Injection Without the Gymnastics [LINK] about DI with Monads and was impressed. I tried to apply it on a simple problem, but failed as soon as it got non-trivial. I really would like to see a running version of dependency injection where a class that depends on more than one value that has to be injected a class that depends on a class that depends on something to be injected as in the following example trait FlyBehaviour { def fly() } trait QuackBehaviour { def quack() } trait Animal { def makeSound() } // needs two behaviours injected class Duck(val flyBehaviour: FlyBehaviour, val quackBehaviour: QuackBehaviou

Recommends ron · 2012-09 · Stack Overflow · on “Finding my way through Scalaz”

Some of the videos I found useful: Scalaz overview by Nick Partridge [LINK] and by Jason Zaugg [LINK] Practical Scalaz by Chris Marshall [LINK] Lenses by Edward Kmett [LINK] (5 parts) Reader monad by Tony Morris [LINK] and by Rúnar Bjarnason [this video] State monad by Michael Pilquist [LINK] Monad transformers by Jordan West [LINK] Most of these have great slides, if you are hardcore then read them without the video. Also learn [LINK] to read Haskell [LINK] type signatures and browse the Haskell typeclassopedia [LINK] .

Recommends Noel Yap · 2012-09 · Stack Overflow · on “How to avoid dependency injection in Scala?”

I read Dependency Injection Without the Gymnastics PDF [LINK] which indicates there's no need for any fancy DI framework, but it's beyond my grasp (at least without concrete examples). I'll try watching Dependency Injection Without the Gymnastics [LINK] and Dead Simple Dependency Injection [this video] when I have a chance. Using Guice in Java, if A depends on both B and C and both B and C depend on D, one would have something like: public class A { @Inject public A(B b, C c) { this.b = b; this.c = c; } } public class B { @Inject public B(D d) { this.d = d; } } public class C { @Inject public C(D d) { this.d = d; } } public class D { /* ... */ } and a module that says which implementation of D to

Recommends ron · 2012-09 · Stack Overflow · on “Good scalaz introduction”

Videos: Scalaz overview by Nick Partridge [LINK] and by Jason Zaugg [LINK] Practical Scalaz by Chris Marshall [LINK] Lenses by Edward Kmett [LINK] (5 parts) Reader monad by Tony Morris [LINK] and by Rúnar Bjarnason [this video] State monad by Michael Pilquist [LINK] Monad transformers by Jordan West [LINK] (Copied from my answer of a closed duplicate).

Recommends Apocalisp · 2014-01 · Stack Overflow · on “How do I give global access to an object in Scala without making it a singleton or passing it to everything?”

When you say "without making it a singleton or passing it to everything", you eliminate your only two options. That said, you can make "passing it to everything" a lot less painful than it sounds, since you only need to pass it to things that actually need to log. See "Dead-simple dependency injection": [this video]

Recommends Kevin Meredith · 2015-11 · Stack Overflow · on “Unit test for Scala object (not class)”

Please check out [this video] Note - although I did not understand this video the first 2-3 times that I saw it, I learned from it the value of simply passing arguments to a function (to achieve DI). You should not need to mock this method using some `mockito` trick.

Recommends Mik378 · 2018-05 · Stack Overflow · on “Scala Currying and function literals”

@Tim It is a curried. Take a look at this video of Runar Bjarnason: [this video] at exactly 3:50 and listen to what he says at that moment. (between 3:50 and 3:55). By the way what your emails.filter(minimumSize(10)) does is a curring since a call of minimumSize(10)(currentEmail) will occur internally.

Cites Apocalisp · 2009-02 · Stack Overflow · on “Understanding the need for a DI framework”

Dependency injection is a degenerate form of implicit parameter passing [LINK] , and the purpose is essentially the same, to solve what's called The Configurations Problem [LINK] : The configurations problem is to propagate run-time preferences throughout a program, allowing multiple concurrent configuration sets to coexist safely under statically guaranteed separation. Dependency Injection frameworks compensate for the lack of implicit parameters [LINK] , Curried functions [this video] , and convenient facilities for monads [LINK] in the language.