scala - 使用蛋糕模式时如何构建 Play 项目

标签 scala playframework projects-and-solutions

下面是 Play 应用程序的常用项目布局:

myProject
    + app
       + controllers
       + models
       + views

我想controllersmodelsviews的内容对我们大多数人来说都很清楚。现在假设我们需要使用蛋糕模式实现一个 DAO 服务:

DaoServiceComponent.scala:

trait DaoServiceComponent[A] {

  def daoService: DaoService

  trait DaoService {

    def insert(entity: A): Future[A]
    def find(id: Id): Future[Option[A]]
    ...
  }
}

trait DefaultDaoServiceComponent[A] extends DaoServiceComponent[A] {
  this: DaoComponent[A] =>

  def daoService = new DaoService {

    def insert(entity: A) = dao.insert(entity)
    def find(id: Id) = dao.find(id)
    ...
  }
}

DaoComponent.scala:

trait DaoComponent[A] {

  def dao: Dao

  trait Dao {

    def insert(entity: A): Future[A]
    def find(id: Id): Future[Option[A]]
    ...
  }
}

UserDaoComponent.scala:

trait UserDaoComponent extends DaoComponent[User] {

  protected val collection: JSONCollection

  def dao = new Dao {

    def insert(entity: User): Future[User] = {
      ...
    }

    def find(id: Id): Future[Option[User]] = {
      ...
    }
  }
}

最后,我们像往常一样将所有内容连接到我们的 Controller 对象中:

object Users extends Controller {

  private val userService: DaoServiceComponent[User]#DaoService =
    new DefaultDaoServiceComponent[User]
    with UserDaoComponent {
      val collection = db.collection[JSONCollection]("users")
    }.daoService

  def create = Action.async(parse.json) { implicit request =>
    request.body.validate[User].fold(
      valid = { user =>
        userService.insert()..
    ...
  }
}

现在回到我们的项目布局,我们应该把 DaoServiceComponentDaoComponentUserDaoComponent 和任何其他支持类放在哪里?这些文件应该放在 services 目录中吗?

myProject
    + app
       + controllers
       + models
       + services
            + DaoComponent.scala
            + DaoException.scala
            + DaoServiceComponent.scala
            + EmailServiceComponent.scala
            + RichEmailComponent.scala
            + ServiceException.scala
            + UserDaoComponent.scala
            + ...
       + views

最佳答案

我们的项目可能不是 Cake 模式的示例性实现,因为它最初是一个没有使用任何依赖项注入(inject)的 Ruby 应用程序,所以对此持保留态度

我们的 DAO 位于模型层/文件夹中 - User.scala 包含一个用户案例类和一个用户 Slick表[用户].

服务层/文件夹中的特征与模型层紧密耦合——我们不注入(inject) DAO。

使用 Cake 模式将服务特征注入(inject)到 Controller 中。我们每个 Controller 都有一个服务,例如,我们有一个 StreamsController 和一个 StreamsService。

我们有大约 12 个 Controller /服务和大约 80 个 DAO(每个 MySql 表一个)。

我们有几个实用程序类,例如 Redis 客户端池和 HTML 模板渲染器 - 它们位于模型层下的 Common 文件夹中,因为其中一些在 DAO 中使用。我们在服务层中有一个 Apps 特征,它包含这些类的抽象值,并且所有服务都通过 self 包含 Apps : Apps => 。然后 Controller 层负责实现和注入(inject)应用程序。

关于scala - 使用蛋糕模式时如何构建 Play 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764860/

相关文章:

playframework - 模板组合/多个 Controller ?

Play Framework 1.3.x 中的 PHP 查询

visual-studio-2008 - Visual Studio 9解决方案中的项目数量是否会影响解决方案的负载和生成时间?

Xcode 将代码移动到新项目

qt - 如何在 Qt Creator 的项目 Pane 中显示嵌套的 pri 文件?

scala - Scala的Range类的独家结束

scala - 确定表达式的值在编译时是否已知

java - IntelliJ Idea 无法解析 Scala 管理源中的符号 (sbt-buildinfo)

java - 如何在 Play 中组织 java 和 scala 代码?

scala - 为什么scala.util.Failure具有类型参数?