Basic Concepts towards understanding of Dagger-2 in android.
Native android development made easy by google dagger libraries. Easy way to understand basic concepts of Dagger and android mobile app development libraries.
2. Introduction
Before starting with DI , Let’s understand the Coupling , Decoupling and
Cohesion.
Coupling :
Tight Coupling :
If two classes make calls to methods of each other then they are
tightly coupled, as changing one would mean having to
change the other.
Decoupling (Low Coupling / Lose Coupling):
Decoupling is the process of making something that was tightly
coupled less so, or not at all.
Cohesion :
Cohesion is the OO principle most closely associated with making sure
that a class is designed with a single
What is DI(Dependency Injection) ?
3.
4. Introduction
The general concept behind dependency injection is called Inversion of Control.
According to this concept a class should not configure its dependencies statically
but should be configured from the outside.
A Java class has a dependency on another class, if it uses an instance of this class.
We call this a _class dependency.
Dependency injection can be performed on:
• The constructor of the class (construction injection)
• A field (field injection)
• The parameters of a method (method injection)
What is DI(Dependency Injection) ?
5. Quick Start On Dagger-2
Gradle Dependencies :
1. compile 'com.google.dagger:dagger:2.x'
2. annotationProcessor 'com.google.dagger:dagger-compiler:2.x' }
Application Class :
final AppComponent appComponent = DaggerAppComponent.builder()
.application(this)
.build();
// DebugLog.e("");
DaggerAutoInject.init(this, appComponent);
6. Quick Start On Dagger-2
Key Points
1. Module
2. Component
3. Scope
To make a class part of dagger object graph, you need to annotate
constructor of the class with Inject annotation. This can be done if you own
the class or creation of object is simple.
7. Quick Start On Dagger-2
Example :
@Singleton
public class Heater {
@Inject
public Heater() {
}
}
@Singleton
public class Thermosiphon {
private Heater heater;
@Inject
public Thermosiphon(Heater heater) {
this.heater = heater;
}
}
8. Quick Start On Dagger-2
Key Points
1. Component :
You cannot use Dagger without any of those, since they contain all the information. I
tend to call components the object graph—objects depending on objects depending on
other objects and so on. Every dependency is a requirement that has to be satisfied
before the dependent object can be created. Thinking of it as a graph thus makes for a
much more understandable picture.
Components know everything about object creation and every object that you want to
use will be delivered to you by a component in some way. I want to start talking about
single components and their usage.
9. Quick Start On Dagger-2
Key Points
1. Module (@module) :
As mentioned above, to make a class part of dagger object graph, you need to
annotate constructor of the class with Inject annotation. This can be done if you
own the class or creation of object is simple.
If you don’t own a class and you want it to be part of dagger object graph, since
constructor can’t be annotated with Inject annotation in this case, you need to
create a method which returns target class type and annotate the method with
@Provides.
@Provides methods are also useful for making classes, which can be instantiated
only using builder classes, to be part of dagger object graph.
Classes which are annotated with Module and contain @Provides annotated
methods are called modules in dagger. Provider methods in modules are used by
dagger to build object graph. Below is an example of Module class.
10. Quick Start On Dagger-2
Key Points
Example :
@Module
public class HeaterModule {
@Provides
@Singleton
Heater heater() {
return new Heater(); // if not using @Inject constructor
}
}
@Module
public class ThermosiphonModule {
@Provides
@Singleton
Thermosiphon thermosiphon(Heater heater) {
return new Thermosiphon(heater); // if not using @Inject constructor
}
}
11. Quick Start On Dagger-2
Key Points
2. Component :
Annotating constructors and fields with Inject is not enough to make
Dagger work. What is needed is an interface for your app’s main code to
get objects from dagger object graph. You need to define the interface
and annotate it with Component so that dagger will generate
implementation of the interface.
In the interface, you need to define methods which return the types you
are interested in. You can name these methods however you want, but
what is important is return type. Below is a component interface example.
Following section explains how to use components and its methods.
12. Quick Start On Dagger-2
Key Points
Example :
@Singleton
@Component // no modules
public interface SingletonComponent {
Thermosiphon thermosiphon();
Heater heater();
void inject(Something something);
}
13. Quick Start On Dagger-2
Key Points
3. Scope:
Singleton annotation is a dagger defined scope. You can use scope annotation on
class and provides-methods in modules to make those instances tied to that scope.
Scope allows you to group instances based on the life time of instances so that
once instances of a particular scope are no longer used, they can be removed more
memory.
For example, instances which are used throughout the application can have
application level scope; instances which are used during a user session can have
session level scope and instances which are used during a flow of task can have
activity level scope. This way instances belong to unneeded scope can be removed
from memory.
14. Quick Start On Dagger-2
Key Points
Example :
Creating Scope & Using Scope
@Scope
@Retention(value= RetentionPolicy.RUNTIME)
public @interface CouponFlowScope {
}
@CouponFlowScope
@Component(modules={CouponModule.class})
public interface CouponComponent {
public void inject(MainActivity mainActivity);
}
15. Quick Questions
1. What is APT ?
Answer:
The android-apt plugin assists in working with annotation processors in
combination with Android Studio. It has two purposes:
• Allow to configure a compile time only annotation processor as a
dependency, not including the artifact in the final APK or library
• Set up the source paths so that code that is generated from the annotation
processor is correctly picked up by Android Studio.
2. What is the user of DI in android ?
Answer :
Let say we want to make some tea. How would we do it and what do we need?
We need some boiled water, a clean mug, teabags, sugar and milk (at least
that’s what I put into mine). In order to make tea we need to provide all these
ingredients and we need to manage all these ourselves. We have to fill the kettle
with water and heat it up, get the milk from the refrigerator and get the rest.
Now assume that we can make tea, by simply asking someone else to do it for
us. Someone that knows how to do the tea just the way we want it. Would that
be better than making the tea ourselves? If yes, then welcome to dependency
injection.
Dependency injection is a framework that takes care of creating objects for us
without having to worry about providing the right ingredients so to say.
16. Quick Questions
3. What are the advantages of Dagger-2 uses ?
Answer:
• Simple access to shared implementations.
• Simple settings of complex dependencies. The big apps usually have
a lot of dependencies. Dagger 2 allows you to control all
dependences easy.
• Simple unit testing and integration testing. We will discuss it in the
article about testing with Dagger 2.
• “Local” singletons.
• Code generation. The received code is clear and available for
debugging.
• No obfuscation problems. Both Point 5 and 6 are advantage
properties of Dagger 2 in comparison with Dagger 1. Dagger 1
worked with reflection. That’s why there were problems with
performance, obfuscation, strange errors in runtime.
17. Quick Revision
@Inject—base annotation whereby the “dependency is requested”
@Module—classes which methods “provide dependencies”
@Provide—methods inside @Module, which “tell Dagger how we want to build
and present a dependency“
@Component—bridge between @Inject and @Module
@Scope—enables to create global and local singletons