1

I have seen a couple of posts related to DI using cake pattern.

One of them being http://jonasboner.com/real-world-scala-dependency-injection-di/ shared by one of my colleagues.

But If I need to use say WSClient from Play 2.5, can I get hold of this without resorting to guice?

Gaurav Abbi
  • 645
  • 9
  • 23

1 Answers1

0

Yes, Scala allows you to do this solely based on language constructs, without using external DI frameworks such as Guice. To illustrate this, consider the following example (this example borrows heavily from the Jonas Bonér blog-post linked in the question):

package di.example

import play.api.libs.ws.WSClient

trait WSClientComponent {
  val wsClient: WSClient
}

NotificationService is a service into which WSClient is to be injected.

package di.example

trait NotificationServiceComponent {this: WSClientComponent =>
  val notificationService: NotificationService

  class NotificationService{
    def getNotifications = wsClient.url("some-url")
  }

}

Next, ComponentRegistry is the "module" object to wire our dependencies together.

package di.example

import play.api.libs.ws.{WS, WSClient}

object ComponentRegistry extends WSClientComponent with NotificationServiceComponent {

  override val wsClient: WSClient = WS.client
  override val notificationService: NotificationService = new NotificationService

}
suj1th
  • 1,781
  • 2
  • 14
  • 22