Johan,
yes, trying to extract myself out of the imperative approach,
something I've managed to do with persistence:
db.handle withSession { implicit ss: Session=>
ss.withTransaction {
val result = for {
u <- safeInsert( User.insert( user ) )
m <- safeInsert( Membership.insert( member ) )
o <- safeInsert( Order.insert( order) )
} yield (u, m, o)
result match {
case Some( (u, m, o) ) =>
Right( (userID, orderID, orderNum) )
case _ => ss.rollback; Left("subscription.not.created")
} } }
however, at the controller layer you may have several conditions to
handle which cannot easily be combined into an elegant control flow
structure. For example, when creating a new subscription the user can
by credit card or by check, so with the above persistence block (in
DAO layer) there are at least 3 conditionals to handle.
It's petty, but I'm not a big fan of indenting down the conditional
rabbit hole. I know I can extract business logic into helper methods,
but my point is more that in some cases, imperative + return is make
for more elegant looking code...whether it is better, that's not a
winnable debate in a language like Scala ;-)
Thanks for the tip, I'm refactoring now, going to see how the fold
with embedded anonymous block works, should be doable, it's a tiny
chunk of code in reality....