Slide 1

Slide 1 text

Dependency Injection & Toothpick https://github.com/stephanenicolas/toothpick

Slide 2

Slide 2 text

2/27 About us Stéphane Nicolas Senior Android Dev at Groupon stephanenicolas snicolas +stephane nicolas Daniel Molinero Android Dev at Groupon dlemures +DaniLemures @D_Lemures

Slide 3

Slide 3 text

• Introduction • Dependency Injection • Libraries • Toothpick • Toothpick Example • Bonus: Advanced scenarios 3/27

Slide 4

Slide 4 text

Dependencies? • Any object can be a dependency • A dependency of X is an object used by X to accomplish its task 4/27 Introduction

Slide 5

Slide 5 text

Dependencies? • Any object can be a dependency • A dependency of X is an object used by X to accomplish its task 4/27 Introduction Snow Cone Granizado Granité Slushie ٩ဉ Granita

Slide 6

Slide 6 text

Dependencies? • Snow Cone Machine. • Dependencies: Ice Machine + Syrup Dispenser 5/27 Introduction

Slide 7

Slide 7 text

Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 6/27 Introduction

Slide 8

Slide 8 text

7/27 Introduction Disadvantages • The class is highly coupled to its dependencies • It depends on its dependencies implementation details • Breaks single responsibility principle • Non reusable code • Non testable code

Slide 9

Slide 9 text

8/27 Dependency Injection What is Dependency Injection (DI)? • Software design pattern • Implements inversion of control to resolve dependencies • Dependencies are passed to the object, it does not build or find them • Separates responsibilities of use and construction • An object is independent of its dependencies implementation details

Slide 10

Slide 10 text

Dependency Injection Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 9/27

Slide 11

Slide 11 text

Dependency Injection Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 9/27

Slide 12

Slide 12 text

Dependency Injection Advantages • Decreases coupling between the object and its dependencies • It is configurable. It does not need to know about a concrete implementation • Reusable code • Testable code 10/27

Slide 13

Slide 13 text

11/27 Libraries But… • Is everything so easy and perfect? • What about something like:

Slide 14

Slide 14 text

11/27 Libraries But… • Is everything so easy and perfect? • What about something like: Can someone do it for me??

Slide 15

Slide 15 text

11/27 Libraries But… • Is everything so easy and perfect? • What about something like: Can someone do it for me?? YES

Slide 16

Slide 16 text

12/27 Libraries Dependency Injection Libraries • They create and inject dependencies for us • Just specify what are the dependencies and how to build them • Two main libraries for Android: • Roboguice • Dagger

Slide 17

Slide 17 text

13/27 Libraries Roboguice • Android oriented • It uses Guice, a Google DI library • Does most of work at runtime • Really simple and powerful solution

Slide 18

Slide 18 text

13/27 Libraries Roboguice • Android oriented • It uses Guice, a Google DI library • Does most of work at runtime • Really simple and powerful solution • Uses reflection heavily, being quite slow • Uses Guava, with thousand of methods, contributing to reaching the dex limit and big APKs

Slide 19

Slide 19 text

14/27 Libraries Dagger • Java and Android oriented • Does most of work at compile time • Reflection replaced by annotation processing • Great performance

Slide 20

Slide 20 text

14/27 Libraries Dagger • Java and Android oriented • Does most of work at compile time • Reflection replaced by annotation processing • Great performance • Harder to use and require boiler plate code • Dependency Injection graph is completely static, defined at compile time

Slide 21

Slide 21 text

15/27 Libraries High level overview Ease of use Speed Roboguice Dagger

Slide 22

Slide 22 text

15/27 Libraries High level overview Ease of use Speed Roboguice Dagger Toothpick

Slide 23

Slide 23 text

16/27 Toothpick What is Toothpick? • Scope tree based Dependency Injection library • Full featured implementation of the JSR 330 • Runtime based, but does not use reflection. Replaced by annotation processing • Almost as fast as Dagger, even faster in some cases • As simple as Roboguice, with few rules • Advanced testing support

Slide 24

Slide 24 text

16/27 Toothpick What is Toothpick? • Scope tree based Dependency Injection library • Full featured implementation of the JSR 330 • Runtime based, but does not use reflection. Replaced by annotation processing • Almost as fast as Dagger, even faster in some cases • As simple as Roboguice, with few rules • Advanced testing support

Slide 25

Slide 25 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 17/27

Slide 26

Slide 26 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 17/27 Specify Dependencies

Slide 27

Slide 27 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 17/27 Specify Dependencies Inject all dependencies and transitive dependencies

Slide 28

Slide 28 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 18/27

Slide 29

Slide 29 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 18/27 Transitive Dependencies

Slide 30

Slide 30 text

Toothpick Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 18/27 Annotated Constructor Transitive Dependencies

Slide 31

Slide 31 text

19/27 Toothpick Snow Cone Machine Syrup Dispenser Snow Cone Machine • Transitive dependencies Ice Machine Pump Transitive Dependency

Slide 32

Slide 32 text

19/27 Toothpick Snow Cone Machine Syrup Dispenser Snow Cone Machine • Transitive dependencies Ice Machine Pump Transitive Dependency

Slide 33

Slide 33 text

20/27 Toothpick Example Requirements • Different flavor Snow Cone Machines: Lemon and Apple • Only one Ice Machine, it is shared • It should be an easy and fast solution:

Slide 34

Slide 34 text

20/27 Toothpick Example Requirements • Different flavor Snow Cone Machines: Lemon and Apple • Only one Ice Machine, it is shared • It should be an easy and fast solution: Toothpick to the rescue!!

Slide 35

Slide 35 text

21/27 Toothpick Example 2 Snow Cone Machines: Lemon and Apple • Scopes are used to define overrides: bindings • Bindings are attached to the Scopes through modules

Slide 36

Slide 36 text

21/27 Toothpick Example 2 Snow Cone Machines: Lemon and Apple • Scopes are used to define overrides: bindings • Bindings are attached to the Scopes through modules

Slide 37

Slide 37 text

21/27 Toothpick Example 2 Snow Cone Machines: Lemon and Apple • Scopes are used to define overrides: bindings • Bindings are attached to the Scopes through modules

Slide 38

Slide 38 text

22/27 Toothpick Example 2 Snow Cone Machines: Lemon and Apple

Slide 39

Slide 39 text

23/27 Toothpick Example Scope tree based D -> E X -> Y A -> B T -> Z

Slide 40

Slide 40 text

24/27 Toothpick Example Only one Ice Machine, it is shared • The Ice Machine is a Singleton • Use @Singleton annotation

Slide 41

Slide 41 text

25/27 Bonus: Advanced scenarios • MVP, keeping the presenter state • Nowadays -> Retained fragments, Loaders, … • Toothpick -> Use a scope to retain the instance of the Presenter • User flow data • Nowadays -> Send data over and over through Intents, Database, … • Toothpick -> Use a common scope for the whole flow to keep flow related data

Slide 42

Slide 42 text

26/27 Conclusion • Toothpick is scope tree based DI framework • Advanced testing support • Nice scenarios: MVP, Flow Scope, … • Fast and simple

Slide 43

Slide 43 text

26/27 Conclusion • Toothpick is scope tree based DI framework • Advanced testing support • Nice scenarios: MVP, Flow Scope, … • Fast and simple How Fast?

Slide 44

Slide 44 text

26/27 Conclusion • Toothpick is scope tree based DI framework • Advanced testing support • Nice scenarios: MVP, Flow Scope, … • Fast and simple How Fast?

Slide 45

Slide 45 text

27/27 Conclusion 1000 injections • Dagger 1: 33 ms • Dagger 2: 31 ms • Toothpick: 35 ms 6400 injections • Dagger 1: 45 ms • Dagger 2: 42 ms • Toothpick: 66 ms

Slide 46

Slide 46 text

The Android Team is Hiring https://jobs.groupon.com/#/categories/engineering