Transitive DI using cake pattern
Asked Answered
R

1

5

I'm trying to do dependency injection using the cake pattern like so:

trait FooComponent {
  val foo: Foo

  trait Foo;
}

trait AlsoNeedsFoo {
  this: FooComponent =>
}

trait RequiresFoo {
  this: FooComponent =>

  val a = new AlsoNeedsFoo with FooComponent{
    val foo: this.type#Foo = RequiresFoo.this.foo
  }

}

but the compiler complains that the RequiresFoo.this.type#Foo doesn't conform to the expected type this.type#Foo.

So the question: is it possible to create a AlsoNeedsFoo object inside RequiresFoo so that dependency injection works properly?

Reprimand answered 24/2, 2014 at 12:14 Comment(0)
S
7

With cake pattern you should not instantiate other components, but extends them.

In your case you if you need functionality of AlsoNeedsFoo you should write something like this:

this: FooComponent with AlsoNeedsFoo with ... =>

And put all together on top level:

val app = MyImpl extends FooComponent with AlsoNeedsFoo with RequiresFoo with ...
Susanasusanetta answered 25/2, 2014 at 3:2 Comment(1)
But that means that it's not possible to create multiple instances of AlsoNeedsFoo (inside RequiresFoo). Is that correct?Reprimand

© 2022 - 2024 — McMap. All rights reserved.