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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -