Scala Cake Pattern and Dependency Collisions

前端 未结 5 2717
情深已故
情深已故 2021-02-20 05:08

I\'m trying to implement dependency injection in Scala with the Cake Pattern, but am running into dependency collisions. Since I could not find a detailed example with such depe

5条回答
  •  心在旅途
    2021-02-20 05:36

    Your final app should look like this:

    object MyApp {
      val fooApi = new FooApiModule {
        val httpClient = new DefaultHttpClient1()
      }.fooApi
      val barApi = new BarApiModule {
         val httpClient = new DefaultHttpClient2()
      }.barApi
      ...
    
     def run() = {
      val r1 = fooApi.foo("http://...")
      val r2 = barApi.bar("http://...")
      // ...
     }
    }
    

    That should work. (Adapted from this blog post: http://www.cakesolutions.net/teamblogs/2011/12/19/cake-pattern-in-depth/)

提交回复
热议问题