Dependency Injection with
Dagger
by Lope Emano
What is Dependency Injection?
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
- James Shore
Dependency Injection is a 25-dollar term for a 5-cent concept.
Dependency injection means giving an object its instance variables.
How to explain dependency injection to
a 5-year old?
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.
“Dependency injection isn’t an end
goal, it is a means to an end - a means
to achieving loosely coupled code”
Loose Coupling
What do you do when your laptop keyboard gets broken?
Tightly coupled code
1. Unscrew laptop
2. Remove cover
3. Remove broken keyboard
4. Position and install new keyboard to data cable
5. Close cover
6. Screw laptop
Loosely coupled code
1. Plug external keyboard to usb port
The usb port
1. Is agnostic to any device that connects to it as long as it
satisfies its specifications
2. USB 3.0 ports are USB 2.0 compatible
3. Today you use it for just about anything- mouse,
trackpad, coffee heaters, lighting, charging phones,
android development, etc..
Let’s see what this looks like in pseudocode! :)
interface USBPluggable{openInputStream(), deviceType()}
class Keyboard implements USBPluggable {}
class AndroidPhoneWire implements USBPluggable {}
class CoffeeCupHeater implements USBPluggable {}
class Keyboard implements USBPluggable {}
interface USBPluggable{openInputStream(), deviceType()}
class Keyboard implements USBPluggable {}
class AndroidPhoneWire implements USBPluggable {}
class CoffeeCupHeater implements USBPluggable {}
class Keyboard implements USBPluggable {}
Code depends upon abstractions instead of
implementations
class Computer(){
Computer(USBPluggable port1){
port1.openInputSream();
port1.deviceType();
}
}
Code that depends upon implementation
instead of abstraction
class Computer(){
Computer(Keyboard keyboardPort){
KeyboardInputStream ks = keyboardPort.openInputSream();
InputStream is = ComputerUtils.convertStream(ks);
}
}
Wait, so what does dagger have to do
with this?
The problem
We need to figure out the dependencies a class needs
We need to figure out how to instantiate them
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
That’s it! Now let’s have a look at documentation
http://square.github.io/dagger/
Non-demo code demo! :)
http://square.github.io/dagger/
https://www.youtube.com/watch?v=IKD2-MAkXyQ
http://www.manning.com/seemann/ - Dependency Injection with Dot Net by Mark Seeman
http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-
year-old
References :

Dependency Injection in Android with Dagger

  • 1.
  • 2.
  • 3.
    is a softwaredesign 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
  • 5.
    - James Shore DependencyInjection is a 25-dollar term for a 5-cent concept. Dependency injection means giving an object its instance variables.
  • 6.
    How to explaindependency injection to a 5-year old?
  • 7.
    When you goand 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.
  • 8.
    “Dependency injection isn’tan end goal, it is a means to an end - a means to achieving loosely coupled code”
  • 9.
  • 10.
    What do youdo when your laptop keyboard gets broken?
  • 11.
    Tightly coupled code 1.Unscrew laptop 2. Remove cover 3. Remove broken keyboard 4. Position and install new keyboard to data cable 5. Close cover 6. Screw laptop
  • 12.
    Loosely coupled code 1.Plug external keyboard to usb port
  • 13.
    The usb port 1.Is agnostic to any device that connects to it as long as it satisfies its specifications 2. USB 3.0 ports are USB 2.0 compatible 3. Today you use it for just about anything- mouse, trackpad, coffee heaters, lighting, charging phones, android development, etc..
  • 14.
    Let’s see whatthis looks like in pseudocode! :)
  • 15.
    interface USBPluggable{openInputStream(), deviceType()} classKeyboard implements USBPluggable {} class AndroidPhoneWire implements USBPluggable {} class CoffeeCupHeater implements USBPluggable {} class Keyboard implements USBPluggable {}
  • 16.
    interface USBPluggable{openInputStream(), deviceType()} classKeyboard implements USBPluggable {} class AndroidPhoneWire implements USBPluggable {} class CoffeeCupHeater implements USBPluggable {} class Keyboard implements USBPluggable {}
  • 17.
    Code depends uponabstractions instead of implementations class Computer(){ Computer(USBPluggable port1){ port1.openInputSream(); port1.deviceType(); } }
  • 18.
    Code that dependsupon implementation instead of abstraction class Computer(){ Computer(Keyboard keyboardPort){ KeyboardInputStream ks = keyboardPort.openInputSream(); InputStream is = ComputerUtils.convertStream(ks); } }
  • 19.
    Wait, so whatdoes dagger have to do with this?
  • 20.
    The problem We needto figure out the dependencies a class needs We need to figure out how to instantiate them
  • 21.
    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
  • 22.
    That’s it! Nowlet’s have a look at documentation http://square.github.io/dagger/
  • 23.
  • 24.
    http://square.github.io/dagger/ https://www.youtube.com/watch?v=IKD2-MAkXyQ http://www.manning.com/seemann/ - DependencyInjection with Dot Net by Mark Seeman http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5- year-old References :