Monday, December 22, 2014

The Cake Is a Lie

In case you don't know yet, the cake pattern is a terrible idea.


I often hear people advising to use the Reader Monad as an alternative, but even if I find the solution conceptually elegant (and I use it often in Haskell), I'm skeptical about it being practical in Scala.
Instead, I was -in my previous company- using mainly implicits with a bunch of ugly Guice refection at the top layer (an artifact from the past more than a principled technological choice).

Having the chance to start a greenfield project recently, I decided to find an alternative to the solutions I did know about... as none of them were really satisfactory.

Talks is cheap, show me the code.

Basically I use implicits until the application layer were I just have one layer of stacked trait... let's see it in action, in this gist we have:
  • common.Module
    • A basic trait to describe modules (set of components)
  • persistence.PersistenceModule
    • A module with two components
  • persistence.postgresql.Database
    • A component
  • web.controllers.Foo
    • A Play! controller
  • web.Global
    • Integration with Play! dependency injection
  • tools.InitializePlatform
    • Integration with a main application


This approach seems to work great so far! Let me know if you give it a try.