加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Slick Repo方法全部参与一个服务的交易

发布时间:2020-12-16 08:53:14 所属栏目:安全 来源:网络整理
导读:我目前有一组以约定* SlickRepo命名的数据访问对象.例如,UserSlickRepo,DistributionSlickRepo,ContentSlickRepo等…… 这些Repos中的每一个都有基于此约定的方法: trait SomethingRepoImpl extends SomethingRepo { val somethingRepo: SomethingRepo = ne
我目前有一组以约定* 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类型代码,而是传入来自服务层的会话?对我来说,这似乎……错了.

解决方法

从我的观点来看,正确的方法是将createSomething和createSomethingElse添加到一个* Repo(SomethingRepo或SomethingElseRepo)事务方法(withTransaction {…}).这不是一个漂亮的解决方案,但对我来说尽可能简单,因为这些实体是逻辑连接的(我们可以从这个代码中看到(dep.somethingRepo.createSomething,dep.somethingElseRepo.createSomethingElse))我认为这不是一个很大的违规行为对一个DAO类中的2个实体进行操作.如果我错了,请修理我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读