scala - Better dao design with Anorm -
i'm working on code base has lots of singleton daos getting new connection pool in each method db.withconnection
executes block.
dao methods use anorm parsers parse result set. there cases each dao method run other dao methods in anorm parsers nested related items business model.
let's assume have data structure this;
user -> posts -> comments each posts
dao work this;
userdao.getuser postdao.getuserposts commentsdao.getpostcomments
because each dao method calling db.withconnection, multiple connections used simple operations.
i want use same connection. can done implicit connection
passing through each dao method. need maintain connection allocation in upper layer. right daos accessed directly rest actions, there not kind of service layer sitting between api , dao. feel it's not have connection around in api layer.
so having services userservice calling daos , handling connection , transaction better.
then requirement make me uncomfortable. of dao methods need called individually.
for example have api requesting comments by;
commentsdao.getpostcomments
this means need implement services daos basically, overriding each dao methods db.withconnection wrapper, seems overhead. (~30 daos, ~10 methods)
another limitation inner dao calls done in anorm parsers, think 1 misuse of library;
when change each dao method definition pass implicit connection
around, parsers fail compile.
because anorm parsers resultsetparser[t]
, there no way pass connection inside default. example parser like;
val apiproviderparser = get[int]("id") ~ get[string]("product_name") ~ get[string]("description") ~ get[string]("icon_url") map { case id ~ productname ~ desc ~ iconurl => { //inner dao call //connection needed val params = getapiproviderparams(id)//(implicit connection) new apiprovidertemplate(id,productname,desc,iconurl,params) } }
maybe custom resultsetparser[t] connection in scope can work, i'm not sure it's correct way of solving problem.
i checked cake pattern, dao design issues couldn't decide how proceed , what's pragmatic, solution problem.
any appreciated.
Comments
Post a Comment