5

I want to use Anorm as DB library and Scaldi as DI in my play framework project. But now I couldn't inject Database since in newest play framwork, we cannot use DB.getConnection("datasource") anymore.

Any of you guys could explain how to convern NamedDatabase to scaldi environment?

Btw, here's my code snipet how to use NamedDatabase in play scala.

class MembershipSqlStorage @Inject() (@NamedDatabase("membership") db: Database)

and it works.

Thanks

cchantep
  • 9,118
  • 3
  • 30
  • 41
Made Raka
  • 85
  • 1
  • 4

1 Answers1

-1

PlayFramework provides useful documentation for Anorm DB library Scaldi DI.

You can refer to :

https://www.playframework.com/documentation/2.6.x/Tutorials

https://www.playframework.com/documentation/2.6.x/PlaySlick

You will need to add Anorm and JDBC plugin to your dependencies :

libraryDependencies ++= Seq( jdbc, "com.typesafe.play" %% "anorm" % "2.5.1" )

First,

"import anorm._"

and then simply use the SQL object to create queries. You need a Connection to run a query, and you can retrieve one from the play.api.db.DB helper with the help of DI -

database.withConnection { implicit c =>

val result: Boolean = SQL("Select 1").execute()

}

To add a Scaldi support in the play application you need to include scaldi-play in the build.sbt:

libraryDependencies += "org.scaldi" %% "scaldi-play" % "0.5.15"

Also https://github.com/playframework/anorm

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – GhostCat Aug 16 '17 at 13:13