Dependency Injection in Android with Dagger

Preview:

Citation preview

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 laptop2. Remove cover3. Remove broken keyboard4. Position and install new keyboard to data cable5. Close cover6. Screw laptop

Loosely coupled code1. Plug external keyboard to usb port

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..

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! :)

Recommended