I am used to frameworks like Ruby on Rails or Laravel in PHP where I have models like User
and when I want to make a query to the model (for example, to get all the users in the database) I do something like User::all()
.
I'm new to Play for Scala and Slick, and all the projects and documentation I've already seen use a DAO for accessing the database through a model. I've thought that a Scala object
is what I need (or at least I am accustomed to) so I don't need to inject DAO classes everywhere.
With a DAO I do something like this:
class Application @Inject()(adminDAO: AdminDAO) extends Controller {
def index = Action.async {
adminDAO.all() map { case admins =>
Ok(Json.toJson(admins))
}
}
}
And with an object (and no idea) I expect to do something like this:
class Application extends Controller {
def index = Action.async {
Admin.all() map { case admins =>
Ok(Json.toJson(admins))
}
}
}
Trying to implement the "DAO" object
I have found almost impossible to to inject the Play Application
context without using Play.current
which is deprecated. That kind of deprecation and the absence of a way of injecting the context (or at least I haven't found it) sounds strange to me. I'm starting to think that I'm thinking it the wrong way. Is it really a good idea to use a Scala object
as DAO?