How can i perform dependency injection without breaking encapsulation?
Using a Dependency Injection example from Wikipedia:
public Car {
public f
Factories and interfaces.
You've got a couple of questions here.
So, what you need is to hide the real code behind an ICar
interface, create a separate EnginelessCar
if you ever need one, and use an ICarFactory
interface and a CarFactory
class to hide the construction details from the consumer of the car.
This will likely end up looking a lot like a dependency injection framework, but you do not have to use one.
As per my answer in the other question, whether or not this breaks encapsulation depends entirely on how you define encapsulation. There are two common definitions of encapsulation that I've seen:
(Code like the first definition can exist in a codebase that works with the second condition - it just tends to be limited to facades, and those facades tend to have minimal or no logic).