scala - Is it possible to use cake pattern based DI for components provided by Play framework? -
i have seen couple of posts related di using cake pattern.
one of them being http://jonasboner.com/real-world-scala-dependency-injection-di/ shared 1 of colleagues.
but if need use wsclient play 2.5, can hold of without resorting guice?
yes, scala allows solely based on language constructs, without using external di frameworks such guice. illustrate this, consider following example (this example borrows heavily jonas bonér blog-post linked in question):
package di.example import play.api.libs.ws.wsclient trait wsclientcomponent { val wsclient: wsclient }
notificationservice service wsclient injected.
package di.example trait notificationservicecomponent {this: wsclientcomponent => val notificationservice: notificationservice class notificationservice{ def getnotifications = wsclient.url("some-url") } }
next, componentregistry "module" object wire our dependencies together.
package di.example import play.api.libs.ws.{ws, wsclient} object componentregistry extends wsclientcomponent notificationservicecomponent { override val wsclient: wsclient = ws.client override val notificationservice: notificationservice = new notificationservice }
Comments
Post a Comment