'Can someone provide a good explanation of Dagger 2?
I'm really having a hard time understanding the Dagger 2 Dependency injection system.
I understand the use of the @Inject
annotation to tell Dagger we need to provide an instance of some type to here.
But, I don't understand the various roles of the other components such as: @Module
, @Component
, @Provides
and how they work together to provide the appropriate instance to the appropriate dependency.
Can someone please explain it simply and concisely ?
Solution 1:[1]
@Module: Modules are classes whose methods provide dependencies, so we define a class and annotate it with @Module, thus, Dagger will know where to find the dependencies in order to satisfy them when constructing class instances. One important feature of modules is that they have been designed to be partitioned and composed together (for instance we will see that in our apps we can have multiple composed modules).
@Component: Components basically are injectors, let’s say a bridge between @Inject and @Module, which its main responsibility is to put both together. They just give you instances of all the types you defined, for example, we must annotate an interface with @Component and list all the @Modules that will compose that component, and if any of them is missing, we get errors at compile time. All the components are aware of the scope of dependencies it provides through its modules.
@Provide: Inside modules we define methods containing this annotation which tells Dagger how we want to construct and provide those mentioned dependencies.
I advise you to read this:
- Tasting Dagger 2 on Android by Fernando Cejas
- Dependency Injection with Dagger 2 (Devoxx 2014) by Jake Wharton
- Dependency Injection with Dagger 2
- Dependency injection with Dagger 2 - the API by froger_mcs
- Dependency injection with Dagger 2 - Custom scopes by froger_mcs
I guess it will help to understand.
Solution 2:[2]
You can find useful Dagger2 sample projects and tutorials here.
Solution 3:[3]
Annotation Used in Dagger 2 and their purpose
@Inject - This is used over the fields, constructor, or method and indicates that dependencies are requested. Here Dagger will construct an instance of the annotated class.
@Module - In simple layman terms, over the class which is used to construct objects and provide the dependencies. Whenever a class is annotated with @Module, Dagger simply knows where to find the dependencies.
@Provide - As the name suggests this guy's job is to provide the instance. This is used over the method in the module class that will return the object.
@Component - Bridge between your Inject & Module. The module class doesn’t provide dependency directly to requesting class, it uses a component interface.
I would definitely recommend going through the links provided by ArtKorchagin.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | ramkrishna kushwaha |
Solution 3 | Mini Chip |