Using Reader Monad for Dependency Injection

前端 未结 1 1627
北荒
北荒 2021-01-31 04:46

I recently saw the talks Dead-Simple Dependency Injection and Dependency Injection Without the Gymnastics about DI with Monads and was impressed. I tried to apply it on a simple

1条回答
  •  孤城傲影
    2021-01-31 05:07

    The "reader monad" is just Function1, so all you need to do is accept an argument containing all the things you need. For example:

    trait Config {
       def fly: FlyBehaviour
       def quack: QuackBehaviour
    }
    
    type Env[A] = Config => A
    

    Now if you want to construct a Duck based on this environment:

    val a: Env[Animal] = c => new Duck(c.fly, c.quack)
    

    And then constructing a Zoo based on that is easy:

    val z: Env[Zoo] = a andThen (new Zoo(_))
    

    Using Scalaz (or with a bit of work on your own) you can make use of some syntax niceties to "ask" for the config c:

    val z: Env[Zoo] = for {
      c <- ask
    } yield new Zoo(Duck(c.fly, c.quack))
    

    0 讨论(0)
提交回复
热议问题