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
like so
Note
DbName("default")
refers todefault
database fromapplication.conf
, for exampleModify
RoomRepo
to takeDatabaseConfig
instead ofDatabaseConfigProvider
like soand mixin
DatabaseComponents
like soCouple of more remarks:
@Inject
with macwire because former is intended for runtime dependency injection whilst latter is for compile-time DI.wire
isdef wire[T]: T
whereT
has to be a type resolved at compile time whilstDatabaseConfigProvider.get[JdbcProfile]
is a runtime value, thereforewire[DatabaseConfigProvider.get[JdbcProfile]]
will not compile.