蛋糕模式:每个实现一个组件,或每个特性一个组件?

Seb*_*ber 4 scala cake-pattern

我正在努力在我的应用程序上使用蛋糕模式.

在网上找到的例子中,例子是基本的,但不涉及更复杂的需求.我想做的事情并不那么花哨:我想在蛋糕模式应用程序中使用相同类型的2个服务,使用不同的实现.

trait UserServiceComponent {
  self: UserRepositoryComponent =>
  val userService: UserService

  class DefaultUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = userRepository.getPublicProfile(id)
  }

  class AlternativeUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = call webservice here for exemple...
  }
}

trait UserService extends RepositoryDelegator[User] {
  def getPublicProfile(id: String): Either[Error, User]
}
Run Code Online (Sandbox Code Playgroud)

如果我一次使用一个实现,它工作正常UserService,但如果我在同一时间需要两个实现,我真的不知道如何做到这一点.

我应该创建2个不同的组件吗?每一个都暴露出不同的userService值名称?(defaultUserService/alternativeUserService).使用一个组件进行实现我不知道其他组件在使用名称时如何能够知道使用哪个实现,userService因为我的应用程序中有2个不同的实现.

顺便说一下,当组件表达UserRepositoryComponent对它的依赖关系时,虽然并非所有实现都需要它,但我发现只有一个组件正确有点奇怪吗?想象一下,我不想构建需要两种实现的完整应用程序,但是我需要为了测试只构建不需要的AlternativeUserService,UserRepositoryComponent因为它不会提供这种依赖关系会很奇怪.用过的.

有人可以给我一些建议,以便我知道该怎么做?

一种相关问题: Cake模式:如何获取组件提供的UserService类型的所有对象

谢谢

Rég*_*les 8

首先,你应该UserServiceComponent从以下的实现中解耦UserService:

trait UserService extends RepositoryDelegator[User] {
  def getPublicProfile(id: String): Either[Error, User]
}

trait UserServiceComponent {
  val userService: UserService
}

trait DefaultUserServiceComponent extends UserServiceComponent { self: UserRepositoryComponent =>
  protected class DefaultUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = userRepository.getPublicProfile(id)
  }
  val userService: UserService = new DefaultUserService
}

trait AlternativeUserServiceComponent extends UserServiceComponent {
  protected class AlternativeUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = call webservice here for exemple...
  }
  val userService: UserService = new AlternativeUserService
}
Run Code Online (Sandbox Code Playgroud)

如果这看起来很冗长,那就好了.蛋糕图案不是特别简洁.

但请注意它是如何解决你的问题,UserRepositoryComponent即使没有实际需要(例如只使用时AlternativeUserService).

现在,我们在实例化应用程序时要做的就是混合使用DefaultUserServiceComponent或者AlternativeUserServiceComponent.

如果您碰巧需要访问这两个实现,那么您确实应该公开两个userService值名称.事实上,3个名字,如:

  • defaultUserService用于DefaultUserService实现
  • alternativeUserService用于AlternativeUserService实现
  • 用于任何UserService实现的mainUserService (应用程序在"混合时间"选择哪一个).

举例:

trait UserService extends RepositoryDelegator[User] {
  def getPublicProfile(id: String): Either[Error, User]
}

trait MainUserServiceComponent {
  val mainUserService: UserService
}

trait DefaultUserServiceComponent { self: UserRepositoryComponent =>
  protected class DefaultUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = userRepository.getPublicProfile(id)
  }
  val defaultUserService: UserService = new DefaultUserService
}

trait AlternativeUserServiceComponent {
  protected class AlternativeUserService extends UserService {
    def getPublicProfile(id: String): Either[Error, User] = ??? // call webservice here for exemple...
  }
  val alternativeUserService: UserService = new AlternativeUserService
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样实例化你的蛋糕:

object MyApp 
  extends MainUserServiceComponent 
  with DefaultUserServiceComponent 
  with AlternativeUserServiceComponent 
  with MyUserRepositoryComponent // Replace with your real UserRepositoryComponent here    
{
  //val userService = defaultUserService
  val mainUserService = alternativeUserService
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,明确要访问的服务DefaultUserServiceDefaultUserServiceComponent作为其组件的依赖(对于AlternativeUserService和相同AlternativeUserServiceComponent),而只需要一些的 服务UserServiceMainUserServiceComponent作为依赖项.您在"混合时间"决定哪个服务mainUserService指向(这里,它指向DefaultUserService实现.