Cake pattern: how to get all objects of type UserService provided by components
Asked Answered
M

1

3

This question may help you understand my needs. Cake pattern: one component per implementation, or one component per trait?

I have a Scala application using multiple UserService implementations which will be provided by component(s?).

I wonder if there is a way in another component to "scan" the application so that I can retrieve a set of all components providing an object which implement the trait UserService? So that I can iterate over all the UserService interfaces provided by my cake built application?

I guess I can have a component which build a list of UserService according to its dependency, but is it possible to have this component building the list without having any hardcoded dependency?

Moan answered 26/1, 2013 at 0:24 Comment(0)
C
6

You can simply have a list of UserService instances right into UserServiceComponent, and have the base UserService register itself in this list.

trait UserServiceComponent {
  private val _userServices = collection.mutable.Buffer[UserService]()
  def userServices: Seq[UserService] = _userServices.synchronized {
    _userServices.toList // defensive copy
  }
  private def registerUserService( service: UserService ) = _userServices.synchronized {
    _userServices += service
  }

  trait UserService {
    registerUserService( this )

    def getPublicProfile(id: String): Either[Error, User]
  }

  val mainUserService: UserService
}

trait DefaultUserServiceComponent extends UserServiceComponent { self: UserRepositoryComponent =>
  protected class DefaultUserService extends UserService {
    // NOTE: no need to register the service, this is handled by the base class
    def getPublicProfile(id: String): Either[Error, User] = userRepository.getPublicProfile(id)
  }
  val mainUserService: UserService = new DefaultUserService
}
Cesta answered 26/1, 2013 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.