Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 examples [closed]

Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1.

Does anyone have an example of a proper application working on Google's Dagger 2?

like image 841
MLProgrammer-CiM Avatar asked Nov 19 '14 20:11

MLProgrammer-CiM


People also ask

What is the use of Dagger 2?

What is Dagger 2? Dagger 2 is a dependency injection for Android. Dagger 2 is a dependency injection (DI) framework. And, Dependency Injection in build upon the concept of Inversion of Control which says that a class should get its dependencies from outside.

What is the use of dagger interface?

This interface defines the connection between the provider of objects (modules) and the objects which express a dependency (consumers). The name of the the generated class is Dagger + the name of the interface. This generated class has a create () method which allows configuring the objects based on the given configuration.

How do I use dagger 2 without component dependencies?

You can use the @Singleton annotation to indicate that there should be only one instance of the object. This is the most basic example of using Dagger 2 without component dependencies, subcomponents, complex modules, or custom scopes. The main Dagger 2 features used are:

How do you inject an object into a component in Dagger 2?

If you want to use field injection you have to define a method in your @Component interface which takes the instance into which you would like Dagger 2 to inject an object into this field. You can use the @Singleton annotation to indicate that there should be only one instance of the object.


2 Answers

I've just published sample app based on Gradle which integrates Dagger2, retrolambda, butterknife and lombok. You can find it here: https://github.com/mgrzechocinski/dagger2-example.

Hope it would help :)

like image 62
mateusz.grzechocinski Avatar answered Oct 16 '22 03:10

mateusz.grzechocinski


I've just ported u2020-mvp app to Dagger 2. We use it as our sandbox app. Dagger 2 has nice implementation of scopes by the way. Components are really big deal. And it is based on u2021 made by Christian Gruber. You can check that as well.

Hope it helps :)

like image 44
Kirill Boyarshinov Avatar answered Oct 16 '22 02:10

Kirill Boyarshinov