Currently browsing

Cake Pattern

Stairs, Cakes and Injections

Dependency Injection is a design pattern meant to increase decoupling, readability and testability by wiring together component implementations so that: All their dependencies on other components’ contracts are satisfied The wiring logic doesn’t belong to any component being wired but to injectors instead Injectors can evaluate wiring rules at runtime Multiple, different injectors can coexist in a single execution Scala supports typesafe Dependency Injection and there are even several ways to implement it. Possibly the most popular one is based on the cake-pattern, which leverages Scala’s self type annotations; yet again I couldn’t find an explanation of it that was crystal clear to me, so I decided to build my own. There are four distinct conceptual layers to this Scala DI implementation strategy …