title: Scala Cake Pattern: Splitting large components into separate files
body: <p>I'd like to use the Cake Pattern for splitting parts of some software system into components to make it completely modular as proposed in <a href="http://lampwww.epfl.ch/~odersky/papers/ScalableComponent.pdf" rel="nofollow">this article</a>. In the simplest case I'd like to have some mockable components, let's say Logging, Config, Database, Scripts etc which could potentially use each other. The code might look like</p>
  
  <pre><code>trait AbstractConfig {
    def config: AbstractConfigInterface
    trait AbstractConfigInterface {
      def test: Unit
    }
  }
  
  trait SomeConfig extends AbstractConfig {
    this: Core =&gt;  
    def config = SomeConfigImplementation
    object SomeConfigImplementation extends AbstractConfigInterface {
      def test = println("conf.test method called")
    }
  }
  
  trait AbstractDatabase {
    def database: AbstractDatabaseInterface
    trait AbstractDatabaseInterface {
      def connect: Unit
    }
  }
  
  trait SomeDatabase extends AbstractDatabase {
    this: Core =&gt;
    def database = SomeDatabaseImplementation
    object SomeDatabaseImplementation extends AbstractDatabaseInterface {
      def connect = {
        println("connect method called")
        core.conf.test
      }
    }
  }
  
  trait Core {
    this: AbstractDatabase with AbstractConfig =&gt;
    def core = CoreInterface
    object CoreInterface {
      def db = database
      def conf = config
    }
  }
  
  object app extends Core with SomeDatabase with SomeConfig
  
  object Run {
    def main(args: Array[String]) = {
      app.core.db.connect
    }
  }
  </code></pre>
  
  <p>Here the database and config components (<code>SomeConfig</code> and <code>SomeDatabase</code> traits) are pluggable and can be changed to some other implementations if ever needed. Their implementations have access to <code>core</code> object which holds both database and config, so the database can access config if needed and vice versa.</p>
  
  <p>So the question is: If ever some trait like <code>SomeDatabase</code> becomes large and doesn't fit into a single file how to split it into separate classes retaining access to the <code>core</code> object? To be more specific, let's say I need to move some code out of connect method in <code>SomeDatabase</code> to another file:</p>
  
  <pre><code>// SomeDatabase.scala
  trait SomeDatabase extends AbstractDatabase {
    this: Core =&gt;
    def database = SomeDatabaseImplementation
    object SomeDatabaseImplementation extends AbstractDatabaseInterface {
      def connect = {
        val obj = new SomeClass()
      }
    }
  }
  
  // SomeClass.scala in the same package
  class SomeClass {
      core.conf.test // Does not compile - how to make it work??
  }
  </code></pre>
  
  <p><code>SomeClass</code> is implementation details of how SomeDatabase works, so I obviously wouldn't like to make it a trait and mix it in to application. Is there any way to provide access to <code>core</code> object for <code>SomeClass</code>?</p>
  
  <hr>
  
  <p>Some related links:</p>
  
  <ol>
  <li><a href="http://www.cakesolutions.net/teamblogs/2011/12/15/dependency-injection-vs-cake-pattern/" rel="nofollow">Dependency Injection vs Cake Pattern by Jan Machacek</a></li>
  <li><a href="http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/" rel="nofollow">Real World Scala: Dependency Injection by Jonas Boner</a></li>
  <li><a href="http://www.warski.org/blog/2010/12/di-in-scala-cake-pattern/" rel="nofollow">Dependency Injection in Scala: Extending the Cake Pattern by Adam Warsky</a></li>
  <li><a href="http://lampwww.epfl.ch/~odersky/papers/ScalableComponent.pdf" rel="nofollow">Scalable Component Abstractions by Martin Odersky &amp; Matthias Zenger</a></li>
  </ol>