What Is Dependency Injection?
Dependency Injection (DI) is a design pattern used in software development to achieve Inversion of Control (IoC) between classes and their dependencies. It allows a class to receive its dependencies from an external source rather than creating them internally. This promotes loose coupling, enhances testability, and improves code maintainability.
Dependencies: These are the objects that a class needs to function. For example, if a class Car
requires an instance of Engine
, then Engine
is a dependency of Car
.
Inversion of Control (IoC): This principle suggests that the control of object creation and management should be inverted. Instead of a class creating its dependencies, an external entity (like a DI container) is responsible for providing them.
Injection Types:
DI Containers: These are frameworks or libraries that manage the creation and injection of dependencies. Examples include Spring (Java), Dagger (Android), and Unity (C#).
Here’s a simple example in Python to illustrate Dependency Injection:
class Engine:
def start(self):
return "Engine started"
class Car:
def __init__(self, engine: Engine):
self.engine = engine
def start(self):
return self.engine.start()
# Dependency Injection
engine = Engine()
car = Car(engine)
print(car.start()) # Output: Engine started
In this example, the Car
class does not create an Engine
instance itself; instead, it receives an Engine
instance through its constructor. This makes it easier to test the Car
class with different types of engines or mock engines.