91

When asked about Dependency Injection in Scala, quite a lot of answers point to the using the Reader Monad, either the one from Scalaz or just rolling your own. There are a number of very clear articles describing the basics of the approach (e.g. Runar's talk, Jason's blog), but I didn't manage to find a more complete example, and I fail to see the advantages of that approach over e.g. a more traditional "manual" DI (see the guide I wrote). Most probably I'm missing some important point, hence the question.

Just as an example, let's imagine we have these classes:

trait Datastore { def runQuery(query: String): List[String] }
trait EmailServer { def sendEmail(to: String, content: String): Unit }

class FindUsers(datastore: Datastore) {
  def inactive(): Unit = ()
}

class UserReminder(findUser: FindUsers, emailServer: EmailServer) {
  def emailInactive(): Unit = ()
}

class CustomerRelations(userReminder: UserReminder) {
  def retainUsers(): Unit = {}
}

Here I'm modelling things using classes and constructor parameters, which plays very nicely with "traditional" DI approaches, however this design has a couple of good sides:

  • each functionality has clearly enumerated dependencies. We kind of assume that the dependencies are really needed for the functionality to work properly
  • the dependencies are hidden across functionalities, e.g. the UserReminder has no idea that FindUsers needs a datastore. The functionalities can be even in separate compile units
  • we are using only pure Scala; the implementations can leverage immutable classes, higher-order functions, the "business logic" methods can return values wrapped in the IO monad if we want to capture the effects etc.

How could this be modelled with the Reader monad? It would be good to retain the characteristics above, so that it is clear what kind of dependencies each functionality needs, and hide dependencies of one functionality from another. Note that using classes is more of an implementation detail; maybe the "correct" solution using the Reader monad would use something else.

I did find a somewhat related question which suggests either:

  • using a single environment object with all the dependencies
  • using local environments
  • "parfait" pattern
  • type-indexed maps

However, apart from being (but that's subjective) a bit too complex as for such a simple thing, in all of these solutions e.g. the retainUsers method (which calls emailInactive, which calls inactive to find the inactive users) would need to know about the Datastore dependency, to be able to properly call the nested functions - or am I wrong?

In what aspects would using the Reader Monad for such a "business application" be better than just using constructor parameters?

4
  • 1
    The Reader monad is not a silver bullet. I think, if you require a lot of levels of dependencies, your design is pretty good. Commented Mar 23, 2015 at 11:00
  • It is however often described as an alternative to Dependency Injection; maybe it should then be described as a complement? I sometimes get the feeling that DI is dismissed by "true functional programmers", hence I was wondering "what instead" :) Either way, I think having multiple levels of dependencies, or rather multiple external services that you need to talk to is how every medium-large "business application" looks like (not the case for libraries for sure)
    – adamw
    Commented Mar 23, 2015 at 11:03
  • 2
    I have always been thought about the Reader monad as something local. For example, if you have some module which talks only to a DB, you can implement this module in the Reader monad style. However, if your application requires many various data sources which should be combined together, I don't think that the Reader monad is good for that. Commented Mar 23, 2015 at 11:28
  • Ah, that could be a good guideline how to combine the two concepts. And then indeed it would seem that DI and RM complement each other. I guess it is in fact quite common to have functions which operate on one dependency only, and using RM here would help to clarify the dependency/data boundaries.
    – adamw
    Commented Mar 23, 2015 at 12:51

3 Answers 3

42

How to model this example

How could this be modelled with the Reader monad?

I'm not sure if this should be modelled with the Reader, yet it can be by:

  1. encoding the classes as functions which makes the code play nicer with Reader
  2. composing the functions with Reader in a for comprehension and using it

Just right before the start I need to tell you about small sample code adjustments that I felt beneficial for this answer. First change is about FindUsers.inactive method. I let it return List[String] so the list of addresses can be used in UserReminder.emailInactive method. I've also added simple implementations to methods. Finally, the sample will use a following hand-rolled version of Reader monad:

case class Reader[Conf, T](read: Conf => T) { self =>

  def map[U](convert: T => U): Reader[Conf, U] =
    Reader(self.read andThen convert)

  def flatMap[V](toReader: T => Reader[Conf, V]): Reader[Conf, V] =
    Reader[Conf, V](conf => toReader(self.read(conf)).read(conf))

  def local[BiggerConf](extractFrom: BiggerConf => Conf): Reader[BiggerConf, T] =
    Reader[BiggerConf, T](extractFrom andThen self.read)
}

object Reader {
  def pure[C, A](a: A): Reader[C, A] =
    Reader(_ => a)

  implicit def funToReader[Conf, A](read: Conf => A): Reader[Conf, A] =
    Reader(read)
}

Modelling step 1. Encoding classes as functions

Maybe that's optional, I'm not sure, but later it makes the for comprehension look better. Note, that resulting function is curried. It also takes former constructor argument(s) as their first parameter (parameter list). That way

class Foo(dep: Dep) {
  def bar(arg: Arg): Res = ???
}
// usage: val result = new Foo(dependency).bar(arg)

becomes

object Foo {
  def bar: Dep => Arg => Res = ???
}
// usage: val result = Foo.bar(dependency)(arg)

Keep in mind that each of Dep, Arg, Res types can be completely arbitrary: a tuple, a function or a simple type.

Here's the sample code after the initial adjustments, transformed into functions:

trait Datastore { def runQuery(query: String): List[String] }
trait EmailServer { def sendEmail(to: String, content: String): Unit }

object FindUsers {
  def inactive: Datastore => () => List[String] =
    dataStore => () => dataStore.runQuery("select inactive")
}

object UserReminder {
  def emailInactive(inactive: () => List[String]): EmailServer => () => Unit =
    emailServer => () => inactive().foreach(emailServer.sendEmail(_, "We miss you"))
}

object CustomerRelations {
  def retainUsers(emailInactive: () => Unit): () => Unit =
    () => {
      println("emailing inactive users")
      emailInactive()
    }
}

One thing to notice here is that particular functions don't depend on the whole objects, but only on the directly used parts. Where in OOP version UserReminder.emailInactive() instance would call userFinder.inactive() here it just calls inactive() - a function passed to it in the first parameter.

Please note, that the code exhibits the three desirable properties from the question:

  1. it is clear what kind of dependencies each functionality needs
  2. hides dependencies of one functionality from another
  3. retainUsers method should not need to know about the Datastore dependency

Modelling step 2. Using the Reader to compose functions and run them

Reader monad lets you only compose functions that all depend on the same type. This is often not a case. In our example FindUsers.inactive depends on Datastore and UserReminder.emailInactive on EmailServer. To solve that problem one could introduce a new type (often referred to as Config) that contains all of the dependencies, then change the functions so they all depend on it and only take from it the relevant data. That obviously is wrong from dependency management perspective because that way you make these functions also dependent on types that they shouldn't know about in the first place.

Fortunately it turns out, that there exist a way to make the function work with Config even if it accepts only some part of it as a parameter. It's a method called local, defined in Reader. It needs to be provided with a way to extract the relevant part from the Config.

This knowledge applied to the example at hand would look like that:

object Main extends App {

  case class Config(dataStore: Datastore, emailServer: EmailServer)

  val config = Config(
    new Datastore { def runQuery(query: String) = List("[email protected]") },
    new EmailServer { def sendEmail(to: String, content: String) = println(s"sending [$content] to $to") }
  )

  import Reader._

  val reader = for {
    getAddresses <- FindUsers.inactive.local[Config](_.dataStore)
    emailInactive <- UserReminder.emailInactive(getAddresses).local[Config](_.emailServer)
    retainUsers <- pure(CustomerRelations.retainUsers(emailInactive))
  } yield retainUsers

  reader.read(config)()

}

Advantages over using constructor parameters

In what aspects would using the Reader Monad for such a "business application" be better than just using constructor parameters?

I hope that by preparing this answer I made it easier to judge for yourself in what aspects would it beat plain constructors. Yet if I were to enumerate these, here's my list. Disclaimer: I have OOP background and I may not appreciate Reader and Kleisli fully as I don't use them.

  1. Uniformity - no mater how short/long the for comprehension is, it's just a Reader and you can easily compose it with another instance, perhaps only introducing one more Config type and sprinkling some local calls on top of it. This point is IMO rather a matter of taste, because when you use constructors nobody prevents you to compose whatever things you like, unless someone does something stupid, like doing work in constructor which is considered a bad practice in OOP.
  2. Reader is a monad, so it gets all benefits related to that - sequence, traverse methods implemented for free.
  3. In some cases you may find it preferable to build the Reader only once and use it for wide range of Configs. With constructors nobody prevents you to do that, you just need to build the whole object graph anew for every Config incoming. While I have no problem with that (I even prefer doing that on every request to application), it isn't an obvious idea to many people for reasons I may only speculate about.
  4. Reader pushes you towards using functions more, which will play better with application written in predominantly FP style.
  5. Reader separates concerns; you can create, interact with everything, define logic without providing dependencies. Actually supply later, separately. (Thanks Ken Scrambler for this point). This is often heard advantage of Reader, yet that's also possible with plain constructors.

I would also like to tell what I don't like in Reader.

  1. Marketing. Sometimes I get impression, that Reader is marketed for all kind of dependencies, without distinction if that's a session cookie or a database. To me there's little sense in using Reader for practically constant objects, like email server or repository from this example. For such dependencies I find plain constructors and/or partially applied functions way better. Essentially Reader gives you flexibility so you can specify your dependencies at every call, but if you don't really need that, you only pay its tax.
  2. Implicit heaviness - using Reader without implicits would make the example hard to read. On the other hand, when you hide the noisy parts using implicits and make some error, compiler will sometimes give you hard to decipher messages.
  3. Ceremony with pure, local and creating own Config classes / using tuples for that. Reader forces you to add some code that isn't about problem domain, therefore introducing some noise in the code. On the other hand, an application that uses constructors often uses factory pattern, which is also from outside of problem domain, so this weakness isn't that serious.

What if I don't want to convert my classes to objects with functions?

You want. You technically can avoid that, but just look what would happen if I didn't convert FindUsers class to object. The respective line of for comprehension would look like:

getAddresses <- ((ds: Datastore) => new FindUsers(ds).inactive _).local[Config](_.dataStore)

which is not that readable, is that? The point is that Reader operates on functions, so if you don't have them already, you need to construct them inline, which often isn't that pretty.

12
  • Thanks for the detailed answer :) One point that's not clear to me, is why Datastore and EmailServer are left as traits, and others became objects? Is there a fundamental difference in these services/dependencies/(however you call them) which causes them to be treated differently?
    – adamw
    Commented Jun 19, 2015 at 14:38
  • Well ... I can't convert e.g. EmailSender to an object as well, right? I wouldn't then be able to express the dependency without having the type...
    – adamw
    Commented Jun 21, 2015 at 22:14
  • Ah, the dependency would then take the form of a function with an appropriate type - so instead of using type names, everything would have to go into the function signature (the name being just incidental). Maybe, but I'm not convinced ;)
    – adamw
    Commented Jun 21, 2015 at 22:15
  • Correct. Instead of depending on EmailSender you'd depend on (String, String) => Unit. Whether that's convincing or not is another issue :) To be certain, it's more generic at least, since everybody already depends on Function2. Commented Jun 22, 2015 at 8:03
  • Well you'd certainly want to name (String, String) => Unit so that it conveys some meaning, though not with a type alias but with something that's checked at compile-time ;)
    – adamw
    Commented Jun 22, 2015 at 10:26
3

I think the main difference is that in your example you are injecting all dependencies when objects are instantiated. The Reader monad basically builds a more and more complex functions to call given the dependencies, wich are then returned to the highest layers. In this case, the injection happens when the function is finally called.

One immediate advantage is flexibility, especially if you can construct your monad once and then want to use it with different injected dependencies. One disadvantage is, as you say, potentially less clarity. In both cases, the intermediate layer only need to know about their immediate dependencies, so they both work as advertised for DI.

5
  • How would the intermediate layer know only about their intermediate dependencies, and not all of them? Could you give a code example showing how the example could be implemented using the reader monad?
    – adamw
    Commented Apr 16, 2015 at 18:54
  • I could probably explain it no better than Json's blog (that you posted) To quote form there "Unlike in the implicits example, we don’t have UserRepository anywhere in the signatures of userEmail and userInfo". Check that example carefully. Commented Apr 16, 2015 at 19:10
  • 1
    Well yes but this assumes that the reader monad you are using is parametrised with Config which contains a reference to UserRepository. So true, it's not directly visible in the signature, but I'd say that's even worse, you have no idea really which dependencies your code is using at first glance. Doesn't being dependent on a Config with all the dependencies mean each method kind of depends on all of them?
    – adamw
    Commented Apr 17, 2015 at 7:01
  • It does depend on them, but it doesn't have to know it. Same as in your example with classes. I see them as fairly equivalent :-) Commented Apr 17, 2015 at 13:19
  • In the example with classes you only depend on what you actually need, not a global object with all dependencies inside. And you get a problem on how to decide what goes inside the "dependencies" of the global config, and what's "just a function". Probably you would end up with a lot of self-dependencies as well. Anyway, that's more a matter-of-preference discussion than a Q&A :)
    – adamw
    Commented Apr 18, 2015 at 18:41
1

The accepted answer provides a great explanation of how the Reader Monad works.

I would like to add a recipe to compose any two functions having varying dependencies using the Cats Library Reader. This snippet is also available on Scastie

Lets define the two functions that we would like to compose: The functions are similar to those defined in the accepted answer.

  1. Define the resources on which the functions depend
  case class DataStore()
  case class EmailServer()
  1. Define the first function with a DataStore dependency. It takes DataStore and returns a List of inactive Users
  def f1(db:DataStore):List[String] = List("[email protected]", "[email protected]", "[email protected]")
  1. Define another function with EmailServer as one of the dependency
  def f2_raw(emailServer: EmailServer, usersToEmail:List[String]):Unit =

    usersToEmail.foreach(user => println(s"emailing ${user} using server ${emailServer}"))

Now the recipe to compose the two functions

  1. First, import the Reader from the Cats Library
  import cats.data.Reader
  1. Change the second function so that it has just one dependency.
  val f2 = (server:EmailServer) => (usersToEmail:List[String]) => f2_raw(server, usersToEmail)

Now f2 takes EmailServer, and returns another function that takes in a List of users to email

  1. Create a CombinedConfig class that contains dependencies for the two functions
  case class CombinedConfig(dataStore:DataStore, emailServer: EmailServer)
  1. Create Readers using the 2 functions
  val r1 = Reader(f1)
  val r2 = Reader(f2)
  1. Change the Readers so that they can work with the combined config
  val r1g = r1.local((c:CombinedConfig) => c.dataStore)
  val r2g = r2.local((c:CombinedConfig) => c.emailServer)
  1. Compose the Readers
  val composition = for {
    u <- r1g
    e <- r2g
  } yield e(u)
  1. Pass the CombinedConfig and invoke the composition
  val myConfig = CombinedConfig(DataStore(), EmailServer())

  println("Invoking Composition")
  composition.run(myConfig)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.