Search code examples
scaladependency-injectiontraitscake-pattern

Scala Cake Pattern Encourage Hardcoded Dependencies?


I'm still trying to learn Scala's Cake Pattern. It seems to me that it gives you the advantage of centralizing your configuration of "Components" as well as the ability to provide default implementations for those components (which are of course overridable).

However, it's use of self-type traits to describe dependencies seems to mix area-of-concerns. The purpose of the Component (I think) is to abstract away the different implementations for that component. But the dependency-listing described in the Component is itself an implementation concern.

For instance, let's say I have a database full of widgets, a registry that allows me to look up specific kinds of widgets, and some sort of algorithm that uses the registry to process the widgets:

case class Widget(id: Int, name:String)

trait DatabaseComponent {
  def database: (Int => Widget) = new DefaultDatabase()

  class DefaultDatabase extends (Int => Widget) {
    // silly impl
    def apply(x: Int) = new Person(x, "Bob")
  }
}

trait RegistryComponent {
  this: DatabaseComponent =>  // registry depends on the database

  def registry: (List[Int] => List[Widget]) = new DefaultRegistry()

  class DefaultRegistry extends (List[Int] => List[Widget]) {
    def apply(xs: List[Int]) = xs.map(database(_))
  }
}

trait AlgorithmComponent {
  this: RegistryComponent =>  // algorithm depends on the registry

  def algorithm: (() => List[Widget]) = new DefaultAlgorithm()

  class DefaultAlgorithm extends (() => List[Widget]) {
    // look up employee id's somehow, then feed them
    // to the registry for lookup
    def apply: List[Widget] = registry(List(1,2,3))
  }
}

And now you can put it together in some central config:

object Main {
  def main(args: Array[String]) {
    val algorithm = new AlgorithmComponent() with RegistryComponent with DatabaseComponent

    val widgets = println("results: " + algorithm.processor().mkString(", "))
  }
}

If I want to change to a different database, I can inject it easily by changing my mixin:

val algorithm = new AlgorithmComponent() with RegistryComponent with SomeOtherDatabaseComponent


But...what if I want to mix in a different Registry component that doesn't use a database?

If I try to subclass the RegistryComponent with a different (non-Default) implementation, the RegistryComponent will insist that I include a DatabaseComponent dependency. And I have to use RegistryComponent, because that's what the top-level AlgorithmComponent requires.

Am I missing something? The moment I use a self-type in any of my Components, I'm declaring that all possible implementations must use those same dependencies.

Has anyone else run into this issue? What is the Cake-like way of solving it?

Thanks!


Solution

  • With the cake pattern, at least by the example I always go to, you should separate the component's interface definition from its default implementation. This cleanly separates the interface's dependencies from the implementation's dependencies.

    trait RegistryComponent {
      // no dependencies
      def registry: (List[Int] => List[Widget])
    }
    
    trait DefaultRegistryComponent extends RegistryComponent {
      this: DatabaseComponent =>  // default registry depends on the database
    
      def registry: (List[Int] => List[Widget]) = new DefaultRegistry()
    
      class DefaultRegistry extends (List[Int] => List[Widget]) {
        def apply(xs: List[Int]) = xs.map(database(_))
      }
    }