This article explains Dependency Injection via Scala's Cake Pattern
.
My understanding of this pattern's benefit is that traits can be mixed in (production v. test) with static checking.
In Mr. Bonér's example, he lists this finished (per example) code:
UserRepositoryComponent and UserServiceComponent
I added comments per my understanding.
trait UserRepositoryComponent {
val userRepository: UserRepository // stand-alone component
class UserRepository {
... // actual implementation here
}
}
trait UserServiceComponent {
this: UserRepositoryComponent => //Requires a mixed-in UserRepo*Component
val userService: UserService
class UserService {
... // actual implementation here
}
}
My understanding is that the Service
depends on injection of a Repository
component.
For production purposes, the following can be used to wire a "production" Repository
component into the UserServiceComponent
:
object ComponentRegistry extends
UserServiceComponent with
UserRepositoryComponent
{
val userRepository = new UserRepository
val userService = new UserService
}
If our production code wanted to use the userRepository
or userService
, is the correct way to use them via a simple import
?
I think that I understand half of the article up to this point, but I'm not sure how to use the ComponentRegistry
object.