dagger2-examples | Examples of dependency injection with Dagger2 | Dependency Injection library
kandi X-RAY | dagger2-examples Summary
kandi X-RAY | dagger2-examples Summary
Examples of dependency injection with Dagger2
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Entry point for reporting .
- Provide Gps sensor .
- Print weather station .
- Provide the weather service .
- Calculate the calibration
dagger2-examples Key Features
dagger2-examples Examples and Code Snippets
Community Discussions
Trending Discussions on dagger2-examples
QUESTION
I am trying to understand an example for an app with offline support using retrofit and room:
You can find the code for it here:
This project is using dependency injections with Dagger2. I've never worked with it so I am trying to understand how things work together. I understand the purpose of dependency injection but I don't understand the implementation of the project above.
I found a very good introduction into Dagger2 here:
A Friendly Introduction to Dagger 2
I worked through it and got most of it. Back to the actual project I am trying to understand (link 2). It still doesn't make sense to me and here is why:
The interface AppComponent has one method which is used:
public void inject(MainActivity2ViewModel viewModelModule);
The return type is void. In the Dagger2 sample project (link 3 and 4) they useWeatherReporter getWeatherReporter();
which makes sense because later they call this method to get a WeatherReporter-Instance and Dagger2 manages all of the instantation process in the background. But I get nothing if the return type is void. Why is the return type not an object?There is one
@Inject
in MainActivity2ViewModel:@Inject public void setRepository(GitHubRepository2 repository) { this.repository = repository; }
repository
is the only field of MainActivity2ViewModel
so it is a dependency. The GitHubRepository2
constructor has 3 parameters:
ANSWER
Answered 2019-Jun-16 at 21:43There's a lot of comprehensive tutorials about Dagger2 in Android. But I'll show you a glimpse of what it's used for. And minimal usage.
Ultimately, dagger will use the annotation @Inject which will provide(reference to the object or value) to the variable.
Injection is usually used on reusable or boilerplate objects like Dao, Repository, ViewModel, NetworkAdapter
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install dagger2-examples
You can use dagger2-examples like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the dagger2-examples component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page