I'm working on a code base has lots of singleton daos which getting a new coection from pool in each method with DB.withCoection then executes block.
Dao methods use Anorm parsers to parse result set. There are some cases each dao method run some other dao methods in anorm parsers to get nested related items for business model.
Let's assume you have data structure like this;
User -> Posts -> Comments for each posts
Dao work like this;
UserDao.getUser
PostDao.getUserPosts
CommentsDao.getPostComments
Because each dao method is calling DB.withCoection, multiple coections used for simple operations.
I want to use same coection. It can be done with implicit coection passing through each dao method. But then I need to maintain coection allocation in upper layer. Right now daos accessed directly from rest actions, there is not any kind of service layer sitting between api and dao. I feel like it's not good to have coection around just in api layer.
So probably having services like UserService which calling daos and handling coection and transaction would be better.
Then another requirement is make me uncomfortable. Most of the dao methods also need to be called by individually.
For example we have api requesting Idea only by;
CommentsDao.getPostComments
This means also I need to implement Services for all daos basically, overriding each dao methods with DB.withCoection wrapper, seems like a overhead. (~30 daos, ~10 methods)
Another limitation is that actually ier dao calls is done in anorm parsers, I think this one is misuse of the library;
When we change the each dao method definition to pass implicit coection around, parsers would fail to compile.
Because anorm parsers is ResultSetParser[T] , there is no way to pass coection inside it as default. Example parser is like;
val apiProviderParser = get[Int]("id") ~
get[String]("product_name") ~
get[String]("description") ~
get[String]("icon_url") map {
case id ~ productName ~ desc ~ iconUrl => {
//Ier dao call
//Coection needed now
val params = getApiProviderParams(id)//(implicit coection)
new ApiProviderTemplate(id,productName,desc,iconUrl,params)
}
}
Maybe custom ResultSetParser[T] with coection in the scope can work, I'm not sure it's correct way of solving this problem.
I checked cake patte, dao design issues but couldn't decide how to proceed and what's the pragmatic, good solution for this problem.
Any help appreciated.
