Dependency injection explained Easily
Dependency injection is a software design pattern that allows an object to receive its dependencies from an external source rather than creating them within the object itself. A dependency is a module, library, or component that an object needs to perform its tasks.
For example, consider a car object in an application. The car object needs an engine to function. Instead of creating the engine within the car object, the engine can be provided to the car object as a dependency. This allows the engine to be reused in other objects as well. The engine can be considered a dependency of the car object.
The process of providing the dependencies to the objects is called injection. In code, this can be achieved through constructors, setters, or interfaces. The main benefit of dependency injection is that it helps to make code more modular, testable, and maintainable.
In summary, dependency injection is the concept of providing objects with their dependencies from external sources, making the code more modular and maintainable.
Dragger
Dragger can be compared to a personal assistant who helps you with tasks. In the same way, Dragger helps you with dependencies in your code. Just as your personal assistant can help you with different tasks, Dragger can provide different dependencies for different parts of your code. This helps you to maintain a clean and organized codebase, as you don’t have to worry about managing the dependencies yourself.
Here is an example of code without dependency injection in Java:
public class Car {
private Engine engine;
public Car() {
engine = new Engine();
}
public void startEngine() {
engine.start();
}
}
public class Engine {
public void start() {
System.out.println("Engine started");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.startEngine(); // Output: Engine started
}
}
In the above code, the Car class creates its own engine when it is instantiated. This makes the code tightly coupled, meaning that if there is any change in the engine class, it will affect the car class as well.
Now, here is an example of the same code with dependency injection using Dagger:
public class Car {
private Engine engine;
@Inject
public Car(Engine engine) {
this.engine = engine;
}
public void startEngine() {
engine.start();
}
}
public class Engine {
@Inject
public Engine() {
}
public void start() {
System.out.println("Engine started");
}
}
@Module
public class EngineModule {
@Provides
Engine provideEngine() {
return new Engine();
}
}
@Component(modules = EngineModule.class)
public interface CarComponent {
void inject(Car car);
}
public class Main {
public static void main(String[] args) {
CarComponent component = DaggerCarComponent.create();
Car car = new Car();
component.inject(car);
car.startEngine();
}
}
In the above code, the car class receives its engine as a dependency via its constructor. The engine is provided by the EngineModule class which is annotated with the @Module
annotation. The CarComponent interface is annotated with the @Component
annotation and it links the EngineModule to the Car class. The Main class creates an instance of the CarComponent and uses it to inject the engine into the car. This makes the code loosely coupled and more maintainable.
LAST
Dagger is a Dependency Injection framework for Android and Java applications. It allows developers to define dependencies between different components in their application and manage those dependencies automatically, without the need for manual instantiation.
Why Use Dagger:
- Improves code organization and readability by clearly defining dependencies.
- Enhances testing by making it easier to replace dependencies with test doubles.
- Increases code reusability by allowing for more modular and maintainable components.
How to Use Dagger:
- Define the dependencies in a module using @Module annotation.
- Create a Component using @Component annotation, which acts as an interface between your code and the dependencies defined in the module.
- Inject the dependencies into the required class using @Inject annotation.
Example: Let’s say we have a class named “MainActivity” which requires a dependency “NetworkService”.
Step 1: Define the dependency in a module:
@Module
public class NetworkModule {
@Provides
NetworkService provideNetworkService() {
return new NetworkService();
}
}
Step 2: Create a Component:
@Component(modules = NetworkModule.class)
public interface NetworkComponent {
void inject(MainActivity mainActivity);
}
Step 3: Inject the dependency into the required class:
public class MainActivity extends AppCompatActivity {
@Inject
NetworkService networkService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerNetworkComponent.create().inject(this);
}
}
Follow, Like and share Thank you