简体   繁体   中英

Inject DatabaseConfigProvider with macwire

I have tried to add Slick to my project

Create RoomRepo class

class RoomRepo @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) extends HasDatabaseConfigProvider[JdbcProfile]{/**...*/}

Inject it

class ChatEngine @Inject()(dbRoom: RoomRepo)(socketIO: SocketIO)(implicit mat: Materializer){/**...*/}

ChatEngine loaded with macwire

trait MyApplication extends BuiltInComponents
  with AssetsComponents
  with SocketIOComponents {
  lazy val dbConfigProvider = wire[DatabaseConfigProvider.get[JdbcProfile]]
  lazy val dbRoom = wire[RoomRepo]
  lazy val chatEngine = wire[ChatEngine]
  lazy val engineIOController: EngineIOController = chatEngine.controller

  override lazy val router = {
    val prefix = "/"
    wire[_root_.router.Routes]
  }
  override lazy val httpFilters = Nil
}

And then I get this error:

[error] /MyApplicationLoader.scala:31:59: type get is not a member of object play.api.db.slick.DatabaseConfigProvider

[error] lazy val dbConfigProvider = wire[DatabaseConfigProvider.get[JdbcProfile]]

[error] ^ [error] /MyApplicationLoader.scala:32:25: Cannot find a value of type: [play.api.db.slick.DatabaseConfigProvider]

[error] lazy val dbRoom = wire[RoomRepo]

[error] ^ [error] /MyApplicationLoader.scala:33:29: Cannot find a value of type: [models.RoomRepo]

[error] lazy val chatEngine = wire[ChatEngine]

play-slick can be used to implement compile-time dependency injection via MacWire by extending SlickComponents

If you're using compile-time DI, you can query the database config directly from the SlickApi using the slickApi.dbConfig(DbName(name)) method. The play.api.db.slick.SlickComponents provide access to the slickApi .

like so

trait DatabaseComponents extends SlickComponents {
  implicit def ec: ExecutionContext
  lazy val dbConfig = slickApi.dbConfig[JdbcProfile](DbName("default"))
  lazy val roomRepo: RoomRepo = wire[RoomRepo]
}

Note DbName("default") refers to default database from application.conf , for example

slick.dbs.default.profile="slick.jdbc.H2Profile$"
slick.dbs.default.db.profile="org.h2.Driver"
slick.dbs.default.db.url="jdbc:h2:mem:play;DB_CLOSE_DELAY=-1"

Modify RoomRepo to take DatabaseConfig instead of DatabaseConfigProvider like so

class RoomRepo(dbConfig: DatabaseConfig[JdbcProfile]))(implicit ec: ExecutionContext) ...

and mixin DatabaseComponents like so

trait MyApplication 
  extends BuiltInComponents
  with AssetsComponents
  with SocketIOComponents
  with DatabaseComponents {

  ...
  implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global
}

Couple of more remarks:

  • We probably should not mix @Inject with macwire because former is intended for runtime dependency injection whilst latter is for compile-time DI.
  • The signature of wire is def wire[T]: T where T has to be a type resolved at compile time whilst DatabaseConfigProvider.get[JdbcProfile] is a runtime value, therefore wire[DatabaseConfigProvider.get[JdbcProfile]] will not compile.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM