Cake drawing: one component per implementation or one component per attribute?

I am currently working on using a cake template in my application.

In the examples I found over the Internet, examples are simple but not related to more complex needs. What I would like to do is not so fantastic: I would like to have a cake template inside the application, 2 services of the same type, using different implementations.

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]
}

It works great if I use one implementation UserServiceat a time, but if I need both implementations at the same time, I really don't know how to do this.

2 ? userService? (DefaultUserService/alternativeUserService). , , , UserService, .

, UserRepositoryComponent, , ? , , , AlternativeUserService, UserRepositoryComponent, , .

- , , ?

: Cake pattern: UserService,

+4
1

, 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
}

, . .

, UserRepositoryComponent, (, AlternativeUserService).

, , , - DefaultUserServiceComponent AlternativeUserServiceComponent.

, userService. , 3 , :

  • defaultUserService DefaultUserService
  • alternativeUserService AlternativeUserService
  • mainUserService UserService ( , " " ).

:

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
}

:

object MyApp 
  extends MainUserServiceComponent 
  with DefaultUserServiceComponent 
  with AlternativeUserServiceComponent 
  with MyUserRepositoryComponent // Replace with your real UserRepositoryComponent here    
{
  //val userService = defaultUserService
  val mainUserService = alternativeUserService
}

, DefaultUserService, DefaultUserServiceComponent ( AlternativeUserService AlternativeUserServiceComponent), , UserService MainUserServiceComponent . " ", mainUserService ( DefaultUserService.

+8

All Articles