Skip to content
0:00 / 0:00

#dependencyinjection#readermonad#scalaz

Dead-Simple Dependency Injection

InfoQ

234K subscribers • 65 videos on Hallway • Conference channel

YouTube
  • Recommended by 4 people: 4 in engineering communities.
  • A classic. People kept recommending it from 2013 to 2019.
  • Presented by Rúnar Óli Bjarnason, senior software engineer at Capital IQ

Intermediate. Assumes working knowledge.

28K views14 years ago4 recommendations

Programming languagesSoftware designScalaDesign patternsFunctional programming

Rúnar Óli Bjarnason explains how to achieve dependency injection in Scala using simple function arguments and the Reader monad.

Rúnar Óli Bjarnason, senior software engineer at Capital IQ and Scalaz library contributor, discusses dependency injection in Scala at the 2012 Northeast Scala Symposium. ** slides available at mrkn.co/4hme2 ** videos sponsored by Typesafe (typesafe.com) and Marakana.

Why it's here

4 people recommend this

What people say. Recommended as a clear explanation of using the Reader monad and simple function arguments for dependency injection in Scala.

u/tpolecatin r/scala • Recommends

On “What is the preferred, common approach to dependency injection?”

I recommend passing dependencies as explicit arguments. This way the dependency is part of the type. You can abstract this away with a simple data type called Reader, or even with basic function types. I do not recommend using implicit parameters for passing dependencies because it is syntactic machinery that doesn't really buy you anything (it's not something you can abstract away). Rúnar gave a good talk about this a few years ago.

u/tpolecatin r/scala • Recommends

On “Trait DI vs Constructor DI”

If you depend on something, take it as an argument. It really does not need to be more complicated than that. If you want to abstract away the plumbing there are all kinds of monads that can do this; the simplest is called Reader. This talk by Rúnar Bjarnason introduces these ideas link to this video

u/koopa1844in r/scala • Recommends

On “A monad is not a burrito”

The Reader monad is useful for composing computations that all depend on a shared value of a certain type, often called the *environment*. For example, say you had some code that talks to your database, and it uses a connection. You have multiple methods in this section of code that all depend on having a connection, and ideally you'd like to call multiple methods in turn (sometimes using the results from previous methods in the input for new ones) and pass them all the same connection object. You could add the connection as a parameter to all of the methods, and manually pass the same connection through all of them as you write your code. But that's a bit cumbersome, and sort of obscures y…

u/kevin_meredithin r/scala • Recommends

On “My highly opinionated list of things needed to build an app in Scala”

Here's a helpful video from nescala 2013 on Dependency Injection in Scala: link to this video by Rúnar Bjarnason.

u/Milyardoin r/scala • Recommends

On “Cake or Guice?”

"Dependency Injection is just a pretentious way of saying 'passing an argument'" - Functional Programming Jesus