dependency injection in android with dagger
TRANSCRIPT
![Page 1: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/1.jpg)
Dependency Injection with Dagger
by Lope Emano
![Page 2: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/2.jpg)
What is Dependency Injection?
![Page 3: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/3.jpg)
is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the dependency inversion and single responsibility principles.
-Wikipedia
![Page 4: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/4.jpg)
![Page 5: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/5.jpg)
- James Shore
Dependency Injection is a 25-dollar term for a 5-cent concept.Dependency injection means giving an object its instance variables.
![Page 6: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/6.jpg)
How to explain dependency injection to a 5-year old?
![Page 7: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/7.jpg)
When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.
What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.
![Page 8: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/8.jpg)
“Dependency injection isn’t an end goal, it is a means to an end - a means to achieving loosely coupled code”
![Page 9: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/9.jpg)
Loose Coupling
![Page 10: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/10.jpg)
What do you do when your laptop keyboard gets broken?
![Page 11: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/11.jpg)
Tightly coupled code
1. Unscrew laptop2. Remove cover3. Remove broken keyboard4. Position and install new keyboard to data cable5. Close cover6. Screw laptop
![Page 12: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/12.jpg)
Loosely coupled code1. Plug external keyboard to usb port
![Page 13: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/13.jpg)
The usb port1. Is agnostic to any device that connects to it as long as it
satisfies its specifications2. USB 3.0 ports are USB 2.0 compatible3. Today you use it for just about anything- mouse,
trackpad, coffee heaters, lighting, charging phones, android development, etc..
![Page 14: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/14.jpg)
Let’s see what this looks like in pseudocode! :)
![Page 15: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/15.jpg)
interface USBPluggable{openInputStream(), deviceType()}
class Keyboard implements USBPluggable {}class AndroidPhoneWire implements USBPluggable {}class CoffeeCupHeater implements USBPluggable {}class Keyboard implements USBPluggable {}
![Page 16: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/16.jpg)
interface USBPluggable{openInputStream(), deviceType()}
class Keyboard implements USBPluggable {}class AndroidPhoneWire implements USBPluggable {}class CoffeeCupHeater implements USBPluggable {}class Keyboard implements USBPluggable {}
![Page 17: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/17.jpg)
Code depends upon abstractions instead of implementations
class Computer(){Computer(USBPluggable port1){
port1.openInputSream();port1.deviceType();
}}
![Page 18: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/18.jpg)
Code that depends upon implementation instead of abstraction
class Computer(){Computer(Keyboard keyboardPort){
KeyboardInputStream ks = keyboardPort.openInputSream();InputStream is = ComputerUtils.convertStream(ks);
}}
![Page 19: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/19.jpg)
Wait, so what does dagger have to do with this?
![Page 20: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/20.jpg)
The problem
We need to figure out the dependencies a class needs
We need to figure out how to instantiate them
![Page 21: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/21.jpg)
A Solution : Dagger
We need to figure out the dependencies a class needs
We need to figure out how to instantiate them
Uses @Inject annotation to specify that this object needs to be injected with a provided instance
Uses @Modules and @Provides annotations to instantiate objects that are to be injected
![Page 22: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/22.jpg)
That’s it! Now let’s have a look at documentation
http://square.github.io/dagger/
![Page 23: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/23.jpg)
Non-demo code demo! :)
![Page 24: Dependency Injection in Android with Dagger](https://reader034.vdocuments.site/reader034/viewer/2022042516/558775d8d8b42a5f6f8b466b/html5/thumbnails/24.jpg)
http://square.github.io/dagger/https://www.youtube.com/watch?v=IKD2-MAkXyQhttp://www.manning.com/seemann/ - Dependency Injection with Dot Net by Mark Seemanhttp://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old
References :