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?
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.