What is dagger exactly, and how it works

What is dependency injection actually?

You will find a lot of technical contents about dependency injection but maximum time new learners find stuck to figure out the basic understanding of DI. Here is a real-life example of DI.

Importance of DI

  • Increase reusabilities of code
  • Good balance of loosely coupled dependancies
  • Ease to refactoring
  • Ease to test and debug

What is dagger?

Dagger is a DI framework that will generate a lot of boilerplate code for you to achieve the goal of dependency injection in Android development.

When to use dagger?

enter image description here

I got a clear vision of DI also with dagger from Android Dev Summit '19 session hopefully which will also clear your vision.


Dagger is a compile-time JSR-330 dependency injection framework based on Java annotations.

What is dependency injection?

Dependency injection (sometimes termed Inversion of Control or IoC) is based on the idea that a given class should not need to know how to create or provide the classes it depends on. Providing those dependencies should be the responsibility of the class's user (or a separate class or factory).

For example, let's say you have class A that depends on class B, that depends on classes C and D. (In real terms, that might be an Application, that depends on a BusinessLogicClass, that depends on a Calculator and a Database.) It could look like this:

class A {
  private B b = new B();
}
class B {
  private C c = new C();
  private D d = new D();
}

...but at that point it would be really hard to use a different C or D (calculator or database), even your B (business logic class) should work with a variety of data sources, or if you need to replace any of those pieces for unit testing. Dependency injection, as a concept, says that you should be able to pass in dependencies into the classes when you create them.

A a = new A(new B(new C(), new D())); // Now you get an A, but can replace any part of it.

What is a dependency injection framework?

A dependency injection framework automatically creates factories for all of your classes, so you can create an A without worrying about its dependencies, or its dependencies' dependencies.

A a = new AFactory().getA(); // Equivalent to the above, but easier!

Dependency injection frameworks usually come with a language for you to specify mappings, often called bindings, which is just a way of instructing that you should create a EImpl when a class asks for a EInterface or a FSubclass when a class asks for a FClass. This language also lets you choose when to create new instances: On every request, once across the whole app ("singleton"), or otherwise. Spring uses XML and Guice uses a custom Java class language for this; Dagger uses Java interfaces, classes, and annotations.

You can do dependency injection without a container or framework, but most of the time people think of a framework like Spring, Guice, or Dagger when you mention dependency injection. These frameworks automate away a lot of the new boilerplate you see above, which could otherwise make "manual" dependency injection hard to work with.

What about JSR 330?

Java standardized some interfaces and annotations as "JSR 330" to make it easier to switch between dependency injection frameworks, and to make it easy to write libraries that work in any framework. Like Spring and Guice, Dagger conforms to this standard.

Why is it a big deal that Dagger is compile-time?

Other dependency injection frameworks, including Spring and Guice, do their configuration at runtime: They use Java reflection to inspect classes' constructors, methods, and fields. This can be slow in production on servers and desktops, and is extremely slow on Android due to both VM differences and mobile processor/memory constraints. Consequently, teams at Square and Google wrote Dagger and Dagger 2 to use Java annotation processing to inspect the classes at compile time and automatically write standard Java code for plain Java objects to act like the factories above. Because this is plain Java without reflection, it can be much faster on Android and embedded systems, and can be optimized using existing tools.

Note, as well, that Dagger was written at Square by Bob Lee (who originally wrote Guice at Google); the Dagger 2 project is rewrite of Dagger maintained at Google that changes some of the specifics about how to configure Dagger and how to avoid using reflection. Both Dagger and Dagger 2 are annotation-based compile-time dependency injection frameworks for Java, but they're slightly different from one another.

Tags:

Android

Dagger