I've been reading a bit about the Cake Pattern in Scala (I know that is old stuff), and I cannot see or imagine an way to lock down the concrete classes, so dependencies don't leak from the classes where they are injected.
A simple and current example comes from the play-reactive-mongo-db activator template. In that app, the controllers use the ReactiveMongoComponents
trait to get an instance of the driver ReactiveMongoApi
.
Now, the problem is that the controller inherits has a public method to return an instance of reactiveMongoApi
and as such breaking 2 fundamental principles: Encapsulation and Information Hiding. I know the example is not great (as a controller shouldn't access any data store), but the same applies if there would be domain services or repositories. I know that those classes (domain services, repositories, etc) would have a trait that defines the accessible methods, and there wouldn't be a public method/attribute to get the dependency, but the concrete implementation does leak.
TL;DR: Cake pattern leaks dependencies from concrete implementations, how can this be avoided?