How to implement the dependency database object?

Learning version 2.5. It's all built on the dependency injection and the documentation not updated. To work with the database using 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
}
}
}


Before DB was a simple object play.api.db.DB , now it is marked @deprecated "DBApi Inject into your component".

Question: how to introduce a dependency into the object? Or it is necessary to change the structure of the models? With dependency injection in controllers, I figured it out. And here came to a standstill. Wasn't the controller to work with the database.
July 9th 19 at 10:35
2 answers
July 9th 19 at 10:37
Solution
Try this
class UserApi @Inject() (db: play.api.db.DBApi ) {
 dababase val = 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
}
Your solution works. Thank you. - enoch.Cremin commented on July 9th 19 at 10:40
July 9th 19 at 10:39
Solution
I found a way to leave the object alone. But this method is more like a crutch:
object User {
 def login(email: String, password: String)(implicit DB: Database): Option[User] = {
 DB.withConnection { implicit c =>
 // TODO
}
}
}

class Application @Inject() (implicit DB: Database) extends Controller {
 def index = Action { User.login(/**/) }
}
Well, kostalek) With classes more correctly and succinctly. Put the UserApi class annotation Singleton and you get almost the same object. In General, my advice is NOT to use anorm. Look in the direction of slick.lightbend.com. - enoch.Cremin commented on July 9th 19 at 10:42
: Why is better not to use Anorm? About slick I know, but I usually either have to write quite cumbersome sometimes specific to this DBMS sql queries or all interaction is through stored procedures. Do I need to look in the direction of slick under such conditions? - berry commented on July 9th 19 at 10:45
: I would like to ask a question. How to use play the console, if all tied to DI? For example, how to call userApi.login() from the console? - enoch.Cremin commented on July 9th 19 at 10:48

Find more questions by tags Play Framework