scala - scala中蛋糕图案的重要性

标签 scala dependency-injection cake-pattern

我已经开始学习 scala 一段时间了,现在正在研究蛋糕图案。我从 here 得到了这个例子

trait UserRepositoryComponent {
  def userLocator: UserLocator

  trait UserLocator {
    def findAll: List[User]
  }
}

trait UserRepositoryJPAComponent extends UserRepositoryComponent {
  val em: EntityManager

  def userLocator = new UserLocatorJPA(em)

  class UserLocatorJPA(val em: EntityManager) extends UserLocator {
    def findAll = {
      println("Executing a JPA query")
      List(new User, new User)
    }
  }
}

trait UserServiceComponent {
  def userService: UserService

  trait UserService {
    def findAll: List[User]
  }
}

trait DefaultUserServiceComponent extends UserServiceComponent {
  this: UserRepositoryComponent =>

  def userService = new DefaultUserService

  class DefaultUserService extends UserService {
    def findAll = userLocator.findAll
  }
}

对我来说,将 JPA 存储库注入(inject)服务似乎有太多样板代码。

然而,这段代码可以用更少的行数做同样的事情
trait UserRepository {
  def findAll
}

trait JPAUserRepository extends UserRepository {
  val em: EntityManager
  def findAll = {
    em.createQuery
    println("find using JPA")
  }
}

trait MyService {
  def findAll
}

trait MyDefaultService extends MyService {
  this: UserRepository=>
}

实例化这两种情况。
val t1 = new DefaultUserServiceComponent with UserRepositoryJPAComponent {
  val em = new EntityManager()
}
t1.userService.findAll


val t2 = new MyDefaultService with JPAUserRepository {
  val em = new EntityManager
}

t2.findAll

第二种情况使用更少的代码,并使用 DI。你能帮我了解蛋糕图案带来的额外优势吗?

最佳答案

据我了解,没有太大区别。其实蛋糕图案是IoC .这只是实现IoC的想法和 DI , 没有单独的 DI框架,但仅使用 scala 代码。您可能应该更喜欢它而不是单独的 DI容器,除非您需要更多功能。

在我看来,你的两个例子都是蛋糕图案。至少我是这么理解的。但是 Martin 在他的书中没有将其命名为“蛋糕模式”,而我对 scala mosly 的了解基于一本书,所以我可能会遗漏一些东西。我的理解是蛋糕模式是结合不同特征来实现的想法DI
我想马丁在他的书中特别提到,使用 DI 是可以的。 -scala中的Spring等容器,但不幸的是我找不到这个地方

更新

找到它:http://www.artima.com/pins1ed/modular-programming-using-objects.html27.1 The problem 的最后一段.但是正如我所说,他在这里不是在谈论“蛋糕”,尽管从您给出的文章中看这个想法是一样的

更新 2

我刚刚重读了我的答案并明白我需要改进它,因为它并没有完全回答这个问题。

您应该更喜欢“蛋糕图案”,因为它更简单。如果你使用Spring,你必须维护配置,无论是XML还是注解,你可能对你的类也有一些要求(我没有使用过Spring,所以我不确定有没有),你有带上整个 Spring 。使用蛋糕模式,您只需编写尽可能简单的代码(您的第二个示例很简单,您应该同意)。 scala 的好处是你可以用它做很多事情,并且只使用几个框架——如果你将它与 java 进行比较——你通常会使用更多的外部库

如果你需要更高级的功能,比如代理——你可以切换到 Spring,或者继续使用 Scala 并用语言本身解决你的问题,希望 scala 非常强大,甚至可以涵盖复杂的情况。

您提供的两个代码片段之间的区别只是抽象:第一个对存储库和服务中定义的操作有一个抽象,这些不是模式的一部分。我觉得这不是必需的,但作者决定这样展示它。

关于scala - scala中蛋糕图案的重要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083670/

相关文章:

c# - 为什么 Scala 没有在匿名类型之间定义合理的相等性?

java - "spark.memory.fraction"好像没有作用

java - 如何在我的自定义 Wicket 模型类中注入(inject) Spring bean?

c# - 如何获取 IServiceCollection 扩展中的依赖项

linux - LWJGL - OpenGL 不渲染

html - 在 Scala Play-Framework 中写入 localStorage?

swift - 通过初始化器注入(inject)依赖项(使用 Storyboard)? [ swift ]

scala - 蛋糕图案和类型

unit-testing - 如何在蛋糕模式中使用模拟

Scala 蛋糕模式和自类型注释