So far when I want to get deep knowledge on Dagger2
I have found lots of good tutorial.
But what I found from there is no one actually describe much about how to cope up with MVP pattern.
There are lots of good example on Github but I found myself confused when explore the code of individual Github projects.
The reason is each project has some different point of view regarding Dagger2.
So After exploring lot of projects I have finally managed to overcome my confusion and made my own way of implementing Dagger2.
This link1 and link2 are the best example for understanding Dagger2
Base elements (annotations) of Dagger 2
@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
@Qualifier — if different objects of the same type are necessary
Below you can see some features of the scope annotations:
-
Usually the scope annotations are set for the Component and provide method.
-
If at least one provide method has a scope annotation the Component should have the same scope annotation.
-
The Component can be unscoped only if all provide methods in all its modules are unscoped too.
-
All scope annotations for one component (for all modules with provide methods to be a part of Component and for the Component itself) should be the same.
Let’s denote Component dependencies traits right away:
- Two dependent components cannot have the same Scope.
- Create an interface with base methods
- Then Create a class that will be injected dependency and implement the interface. The constructor injection is
applied here by
@Inject
annotation on constructor.
- Now Create a Class with
@Module
annotation and create a method with@Provides
- Now use
Method Injection
to get the object of the injected class.
This project has divided into 2 different layer
- presentation
- data
Currently we don't focus on domain layer.
Thanks
Anjan Debnath