Dagger: a fast Java dependency injection

marcossilvano
Quick Mobile Labs
Published in
3 min readSep 6, 2016

Dagger is a Java framework to manage dependency injection. It is relatively easy to set up. All work is done in compile time and all code generated is legible. Besides the dependencies are linked by graphs of components. By using Dagger you make your code more testable and also make it cleaner (because you’re going to avoid block of instances).

Usage

Dagger works injecting dependencies on annotated elements. If you have complex classes with some configuration, you should start the system of graphs with these components, so dagger can use them.

The simplest injection is declared by the annotation `@Inject`. This annotation can be used in constructor methods, attributes and other methods (however this last practice isn’t recommended).

As you can see in the code below, we are creating a instance of the AClass using the annotation @Inject . If the AClass’s constructor method has parameters, you will need to use @Inject in the construtor, otherwise, there is no need.

@Inject works for simplest cases. It wasn’t created to be used in interfaces, third-party classes or configurable objects.

In the case of configurable objects, you can create them as follows: add ‘@Module’ note in class and ‘@Provides’ note in configurable method. This configurable object may or may not have parameters. After doing so, you will have a component, however, it has not been included in the graph components yet.

In order to add the component in the graph you must use an interface. Simply put ‘@Component’ annotation on it and pass as the class a parameter with ‘@Module’ annotation (see example below).

The Dagger will create a class (in our example, DaggerSomeClass_Cinterface.class) that we will be used to insert the component in the graph.
If the constructor and methods annotated using @Provides are public and the constructor has no parameter, you can use .create () instead of .builder().build(). It will return an instance of the CClass which may be used to access the added components in the graph.

If the Module has a constructor, hIf happens that the module have a constructor it must be started passing a instance of the module as a parameter to a method with the same name as the instance of the module.

If the Module has a constructor, you will need to do something like this:

It is really important to know that a component may have a subcomponent, and the interface annotated (`@Component`) can have multiples methods. Besides, it’s possible to inject a list or a map of dependencies by using `@IntoSet` and `@IntoMap`. Theses structures will be added into the graph and they can be accessed as set and map.

Dagger also provides annotations for a more flexible control of components in graph:

  • `@Singleton` annotation define that each instance will be unique to the client.
  • `@Reusable` annotation define that each instance can be reused and not bind an instance to a module, but is restricted in a same component.
  • `Lazy<SomeClass>` is used in @Inject field, this element make all requests of instance return the same instance.
  • `Provider<SomeClass>` as lazy is used with annotated @Inject, but unlikely the Lazy each request will receive a new instance.

--

--