【发布时间】:2015-09-08 08:10:16
【问题描述】:
我目前有一组以约定*SlickRepo 命名的数据访问对象。比如UserSlickRepo、DistributionSlickRepo、ContentSlickRepo等等……
每个 Repos 上都有基本遵循此约定的方法:
trait SomethingRepoImpl extends SomethingRepo {
val somethingRepo: SomethingRepo = new SomethingRepoImpl
class SomethingRepoImpl extends SomethingRepo with MySlickDatastore {
def getSomething(id: UUID): Either[SomethingNotFoundError, Something] = {
getDatabase withDynSession {
// Slick stuff
}
}
def createSomething .....
}
}
现在在服务级别,我们在这个 repo 类中烘焙,我们有如下所示的方法:
trait SomethingServiceImpl extends SomethingService {
dep: SomethingRepo with SomethingElseRepo =>
val somethingService = new SomethingServiceImpl
class SomethingServiceImpl extends SomethingService {
def createSomethingGood(): Either[SomeError, (Something, SomethingElse)] = {
(dep.somethingRepo.createSomething, dep.somethingElseRepo.createSomethingElse)
}
}
}
我们现在希望createSomethingGood 在一个事务中实际运行两个 repo 方法。由于所有 Slick 的东西都被锁定在 Slick 特定的 Repo 方法中,那么最好的方法是什么?我不反对在我的*ServiceImpl 类中使用特定于 Slick 的代码(我的意思是很奇怪,但没关系),但这是否意味着我必须更改我的 Repo 类以一起删除 getDatabase withDynSession 类型代码,而是通过在服务层的会话中?对我来说,这似乎......错了。
【问题讨论】:
-
您可以拥有一个带有隐式会话的事务服务suggested here。旁注:你确定你的 Repos 实际上是 Repositories,而不是 DAO 对象?