際際滷

際際滷Share a Scribd company logo
Dependency Injection
Using Dagger 2
What is Dependency Injection ?
Its Famous and everybody is talking about it.
You might be using it without writing the independent examples.
Butterknife is the hint
What is Dependency ?
Class Car{
getPermittedSpeed(String country){
TrafficRules rules=new Traficrules(); // Dependency
rules.getSpeed(country);
}
getAgeLimit(String country){
TrafficRules rules=new Traficrules(); // Dependency
rules.getAgeLimit(country);
}
}
Class Car{
getPermittedSpeed(String country,TrafficRules rule){
rule.getSpeed(country);
}
getAgeLimit(String country, TrafficRules rule){
rule.getAgeLimit();
}
}
This is Dependency Injection
// Only god can create perfectly not you, so dont create us simply call us
Class Car{
TrafficRules rules;
Car(){
rules=new TrafficRules();
}
// Dependency Resolved
getPermittedSpeed(String country){
rules.getSpeed(country);
}
getAgeLimit(String country)...
}
Wrong ! You cannot create you
should only call
Class Car{
TrafficRules rules;
Car(TrafficRules rules){
this.rules= rules;
}
// Dependency Resolved
getPermittedSpeed(String country){
rules.getSpeed(country);
}
getAgeLimit(String country)...
}
Now you are a good guy!
This is correct as we are not
creating but calling
Someone have to do the gods job of Creation!
Class Car{
@Inject
TrafficRules rules; // Will be done by Libraries like Dagger ?
// Dependency Resolved
getPermittedSpeed(String country){
rules.getSpeed(country);
}
}
Welcome to Dagger 2
How did dagger2 know to create object ?
Secret: You will tell Dagger2, what objects it has to
create
Dagger2 usage
Tell 3 Things to Dagger2
> What are the Objects Dagger 2 has to create for you
> What functions it has call for creating these Objects ?
> Where all it has to inject these pre created Objects
Dagger2 usage
Tell 3 Things to Dagger2
> What are the Objects Dagger 2 has to create for you
> What functions it has call for creating these Objects ?
> Where all it has to inject these pre created Objects
@Modules
@Provides
@Component
THANK
YOU

More Related Content

Dependency Injection in Android with Dagger 2

  • 2. What is Dependency Injection ? Its Famous and everybody is talking about it. You might be using it without writing the independent examples. Butterknife is the hint
  • 3. What is Dependency ? Class Car{ getPermittedSpeed(String country){ TrafficRules rules=new Traficrules(); // Dependency rules.getSpeed(country); } getAgeLimit(String country){ TrafficRules rules=new Traficrules(); // Dependency rules.getAgeLimit(country); } }
  • 4. Class Car{ getPermittedSpeed(String country,TrafficRules rule){ rule.getSpeed(country); } getAgeLimit(String country, TrafficRules rule){ rule.getAgeLimit(); } } This is Dependency Injection // Only god can create perfectly not you, so dont create us simply call us
  • 5. Class Car{ TrafficRules rules; Car(){ rules=new TrafficRules(); } // Dependency Resolved getPermittedSpeed(String country){ rules.getSpeed(country); } getAgeLimit(String country)... } Wrong ! You cannot create you should only call
  • 6. Class Car{ TrafficRules rules; Car(TrafficRules rules){ this.rules= rules; } // Dependency Resolved getPermittedSpeed(String country){ rules.getSpeed(country); } getAgeLimit(String country)... } Now you are a good guy! This is correct as we are not creating but calling
  • 7. Someone have to do the gods job of Creation!
  • 8. Class Car{ @Inject TrafficRules rules; // Will be done by Libraries like Dagger ? // Dependency Resolved getPermittedSpeed(String country){ rules.getSpeed(country); } } Welcome to Dagger 2
  • 9. How did dagger2 know to create object ? Secret: You will tell Dagger2, what objects it has to create
  • 10. Dagger2 usage Tell 3 Things to Dagger2 > What are the Objects Dagger 2 has to create for you > What functions it has call for creating these Objects ? > Where all it has to inject these pre created Objects
  • 11. Dagger2 usage Tell 3 Things to Dagger2 > What are the Objects Dagger 2 has to create for you > What functions it has call for creating these Objects ? > Where all it has to inject these pre created Objects @Modules @Provides @Component