dependency injection and dagger2 in android paramvir singh

17

Click here to load reader

Upload: paramvir-singh

Post on 21-Jan-2018

161 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Dependency injection and dagger2 in android   paramvir singh

Dependency

injectionAND DAGGER2 FRAMEWORK IN ANDROID

Page 2: Dependency injection and dagger2 in android   paramvir singh

WHAT IS DEPENDENCY?

Page 3: Dependency injection and dagger2 in android   paramvir singh

INJECTION

Push dependency from outside into the class.

• Fetch dependency

• Receive dependency

Page 4: Dependency injection and dagger2 in android   paramvir singh

DI

Ask third party to create object, instead of create dependencies

yourself.

Page 5: Dependency injection and dagger2 in android   paramvir singh

WHY NEED DI

Decouple classes construction from construction of your

dependency.

Page 6: Dependency injection and dagger2 in android   paramvir singh

DEPENDENCY INVERSION PRINCIPLE

Class Dependency

Interface

Implementation

Dependen

cy

Page 7: Dependency injection and dagger2 in android   paramvir singh

DI CONTAINER

Create and map dependency

Page 8: Dependency injection and dagger2 in android   paramvir singh

DI LIBRARY FOR ANDROID

• Dagger 2

Page 9: Dependency injection and dagger2 in android   paramvir singh

Dagger 2

Dagger 2 analyzes dependencies for you and generates code to

help wire them together.

Page 10: Dependency injection and dagger2 in android   paramvir singh

WITHOUT DAGGER 2

Activities

Fragments

Views

Objects

API interface

(Retrofit)

Image Loader

(Picasso)

Shared

Preference

Network client

Image disk

cache

Page 11: Dependency injection and dagger2 in android   paramvir singh

DAGGER USAGE

Components ObjectFind object

@Inject

Take all fields with annotation, look in the component and find the object by

type, and set the field.

Page 12: Dependency injection and dagger2 in android   paramvir singh

DAGGER USAGE

@Inject Picasso Picasso;

@ Inject Preference preference onject

// in oncreate function

getComponent().inject(this)

Take all fields with annotation, look in the component and find the object by type, and set

the field.

Page 13: Dependency injection and dagger2 in android   paramvir singh

ANNOTATIONS USED BY DAGGER 2

• @Module and @Provides

• @Inject

• @Components

Page 14: Dependency injection and dagger2 in android   paramvir singh

MODULES & COMPONENTS

Activities

Fragmen

ts

Views

Objects

Network client

Shared

Preferences

Application

Network module

Storage module

Application

module

Applicati

on

compon

ent

.inject(

)

Page 15: Dependency injection and dagger2 in android   paramvir singh

SCOPES

• @UserScope

• @Singleton

• @ActivityScope

Page 16: Dependency injection and dagger2 in android   paramvir singh

CONSTRUCTOR INJECTION

@Provides

public StoreUserInfo

provideStorUserInfo(NetworkApi networkApi){

return new StoreUserInfo(networkApi);

}

public class MainActivity extends

AppCompatActivity {

@Inject

StoreUserInfo storeUserInfo;

@Override

protected void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

((MyApplication)

getApplication()).getComponent()

.inject(this);

}

Page 17: Dependency injection and dagger2 in android   paramvir singh

Contact:

Paramvir Singh

[email protected]