29
Getting Started with Dagger 2 Fast dependency injection for Android applications

Getting started with dagger 2

Embed Size (px)

Citation preview

Getting Started with Dagger 2

Fast dependency injection for Android applications

Why dependency injection?

Separation of concerns

Testability

Every app has some kind of dependency injection

Uncle Bob

“Depend upon Abstractions. Do not depend upon concretions.”

A brief history of time

JSR-330

Guice

Dagger

Dagger 2

Maintained by Java Core Libraries team at Google

Faster than Guice and Dagger

Easy to use and to debug

Mobile first

Dagger API

@Modules + @Provides

@Inject

@Component

Providing Dependencies

Requesting Dependencies

@Inject annotations required

Constructor, Field & Method

Constructor Injection

@Inject on a single constructor

Parameters are dependencies

They can be stored in private and final fields

Constructor Injection

Constructor Injection

It’s useless now

Constructor Injection

Dependency Graph

EpisodeRepository DataModule#provideEpisodeRepository

RemoteEpisodeRepository

TraktvApi DataModule#provideTraktvApi

OmdbApi DataModule#provideOmdbApi

Constructor Injection

Field Injection

@Inject on fields for dependencies

Field can not be private or final

Inject happens after object is fully instantiated

Object is usually responsible for injection

Method Injection

http://google.github.io/dagger/

Components

Bridge between modules and injections

The injector

Components

Components

this notation is optional

Components

Components

How about some live coding?

github.com/google/dagger

Questions?

Questions?

github.com/rodrigohenriques