Scala Cake Pattern and Dependency Collisions

前端 未结 5 2689
情深已故
情深已故 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:27

    You get the pattern correctly and you've just discovered its important limitation. If two modules depend on some object (say HttpClient) and happen to declare it under the same name (like httpClient), the game is over - you won't configure them separately inside one Cake. Either have two Cakes, like Daniel advises or change modules' sources if you can (as Tomer Gabel is hinting).

    Each of those solutions has its problems.

    Having two Cakes (Daniel's advice) looks well as long they don't need some common dependencies.

    Renaming some dependencies (provided it's possible) forces you to adjust all code that uses those.

    Therefore some people (including me) prefer solutions immune to those problems, like using plain old constructors and avoid Cake altogether. If you measured it, they don't add much bloat to the code (Cake is already pretty verbose) and they're much more flexible.

提交回复
热议问题