Answer the question
In order to leave comments, you need to log in
How to inject a DB dependency into an object?
I'm studying version 2.5. It's all built on dependency injection, and the documentation hasn't been updated. To work with the database, I use Anorm:
case class User(id: Long, login: String, password: String, email: String )
object User {
def login(email: String, password: String): Option[User] = {
DB.withConnection { implicit c =>
// TODO
}
}
}
play.api.db.DB
, now it's marked with @deprecated "Inject DBApi into your component". Answer the question
In order to leave comments, you need to log in
Try like this
class UserApi @Inject() (db: play.api.db.DBApi ) {
val dababase = db.database("name")
def login(email: String, password: String): Option[User] = {
dababase.withConnection { implicit c =>
// TODO
}
}
}
//controller
class MyController @Inject() (userApi: UserApi ) extends Conroller {
//your code
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question