I'm currently working to use the cake pattern on my application.
On exemples I have found across the web the exemples are kind of basic but doesn't involve more complex needs. What I'd like to do is not so fancy: I would like to have inside a cake pattern 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 fine if I use one implementation of the UserService
at a time, but if I need both implementations in the same time, I don't really know how to do it.
Should I create 2 distinct components? Each one exposing a different userService value name? (defaultUserService/alternativeUserService). Using one component for both implementation I don't know how other components would be able to know which implementation is used when using the name userService
since there are 2 distinct implementations in my application.
By the way, as the component expresses the dependency to the UserRepositoryComponent
, while it is not needed by all implementations, I find it a bit weird to have only one component right?
Imagine I don't want to build the full application which needs both implementations, but I need, for tests, to build only the AlternativeUserService which doesn't need the UserRepositoryComponent
, it would be weird to have to provide this dependency as it will not be used.
Can someone give me some advices so that I know what to do?
Kind of related question: Cake pattern: how to get all objects of type UserService provided by components
Thanks