I'm working with dependency injection at the moment. Effectively this involves the UI layer (e.g. a web application) including a DI container which has a whole bunch of data about interfaces it will work with, and which implementation to use for each.
However, because of the need to satisfy dependencies, the DI container also needs to know about interfaces and classes it would not otherwise need access to. For example:
The UI layer needs to work with an IWidgetManager
interface. The configured implentation is ConcreteWidgetManager
. This is registered in the DI container.
The ConcreteWidgetManager
has a dependency on IWidgetRepository
. The desired implementation for this is ConcreteWidgetRepository
. Thus the DI container must also know about IWidgetRepository
and ConcreteWidgetRepository
.
Had I simply hardcoded ConcreteWidgetManager
's dependency on ConcreteWidgetRepository
, then ConcreteWidgetRepository
could be made internal and therefore invisible to my UI layer. No UI code could bypass the manager layer and work directly with the repository layer.
Therefore it seems that the use of DI, although an awesome pattern in many respects, defeats encapsulation from the point of view of the UI layer.
Am I right in thinking this, and is there any way to mitigate the situation?