Slide 1

Slide 1 text

Toothpick: 
 scope tree 
 Dependency Injection https://github.com/stephanenicolas/toothpick

Slide 2

Slide 2 text

2/40 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

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

Slide 4

Slide 4 text

4/40 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 5

Slide 5 text

• Introduction • TP Vs Daggers: Even sharper ! • Toothpick Features • Basic Injections • Scope Tree of an Android App • Configurations • Testing • Flows & Scopes • Conclusion Plan 5/40

Slide 6

Slide 6 text

Roboguice - Runtime time DI • friendly syntax, simple • too slow (uses reflection) • too big (dex methods count & APK size) Why Toothpick ? Daggers - Compile time DI • Fast ! • tough syntax. Hard to use & maintain • No easy testing 6/40

Slide 7

Slide 7 text

7/40 Why Toothpick ? Ease of use Speed Roboguice Dagger Toothpick Best of both worlds

Slide 8

Slide 8 text

8/40 Toothpick Vs. Daggers Speed comparison

Slide 9

Slide 9 text

9/40 Toothpick Vs. Daggers 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 Speed comparison in a reasonable range TODO redo the graph for 0 injection

Slide 10

Slide 10 text

10/40 Toothpick Vs. Daggers Ease of use TP gains over Dagger 2: • Lines of Code: • No need for component interfaces • No special rules to passover stuff to sub- components • More flexible use of field / constructor injections • Less rules to learn, easier to use.

Slide 11

Slide 11 text

Dependencies? • Snow Cone Machine. • Dependencies: Ice Machine + Syrup Dispenser 11/40 Basic Injection

Slide 12

Slide 12 text

Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 12/40 Basic Injection

Slide 13

Slide 13 text

Basic Injection : @Inject Snow Cone Machine • Dependencies: Ice Machine + Syrup Dispenser 13/40 Specify Dependencies SnowConeMachine uses @Inject to specify its dependencies. It can use Lazy to lazily create some dependencies.

Slide 14

Slide 14 text

Basic Injection : Scopes 14/40 Scope Instance creations take place inside scopes SnowConeMachine IceMachine SyrupDispenser All dependencies of the SnowConeMachine are injected.
 Transitively, all dependencies of the dependencies are created as well…

Slide 15

Slide 15 text

Basic Injection : Scopes 15/40 Scope Injections take place inside scopes When we don’t have control on the creation of an entry point, we can inject its fields inside a scope. It will also create transitively all dependencies.

Slide 16

Slide 16 text

Basic Injection : Scopes & Bindings 16/40 Scope Scopes contain bindings. Bindings are configured via modules. 
 IceMachine’s instance is a singleton of this scope. Module 1 Module 2 IceMachine —> new IceMachine SyrupDispenser —> LemonSyrupDispenser

Slide 17

Slide 17 text

Basic Injection : Scopes & Bindings 17/40 Bindings describe what is to be injected.
 Bindings can recycle instances → Singletons. SnowConeMachine IceMachine Lemon
 SyrupDispenser

Slide 18

Slide 18 text

18/40 Scope annotations • The Ice Machine is a Singleton • Use @Singleton annotation or a module = Scope IceMachine
 —> new IceMachine =

Slide 19

Slide 19 text

19/40 Scope annotations TODO custom scope annotations. Present singleton as a special case.

Slide 20

Slide 20 text

Scope tree of an Android App 20/40 Application MakeLemonCones
 Activity MakeAppleCones
 Activity SyrupDispenser
 —> LemonSyrupDispenser SyrupDispenser
 —> AppleSyrupDispenser IceMachine
 —> new IceMachine IceMachine is a Singleton at the App level. SyrupDispenser is bound to different implementations in each activity scope.

Slide 21

Slide 21 text

Scope tree of an Android App 21/40 Application IceMachine
 —> new IceMachine

Slide 22

Slide 22 text

Scope tree of an Android App 22/40 Application MakeLemonCones
 Activity When opening multiple scopes at the same time, 
 they will be children from each other, in left to right order. The last child scope is returned.

Slide 23

Slide 23 text

23/40 Toothpick Modules 2 Snow Cone Machines: Lemon and Apple Each activity will define its own scopes, that defines different bindings

Slide 24

Slide 24 text

Scope tree of an Android App 24/40 Application Activity 2 TP scope tree is totally extensible. It can be printed at anytime. Activity 1 Fragment 2 View 1 Fragment 1 Service 2 Service 1

Slide 25

Slide 25 text

Configurations 25/40 TODO slide with all possible configurations

Slide 26

Slide 26 text

Configurations 26/40 TODO slide to explain registries

Slide 27

Slide 27 text

Configurations 27/40 Performance of Registries Vs. Reflection

Slide 28

Slide 28 text

Testing: simple tests 28/40

Slide 29

Slide 29 text

Testing: simple tests 29/40 Chain Junit4 
 rules Prepare m ocks 
 & stuff under test Enjoy

Slide 30

Slide 30 text

Testing: even simpler tests 30/40

Slide 31

Slide 31 text

Testing: Robolectric tests 31/40

Slide 32

Slide 32 text

32/40 Chain Junit4 
 rules override activity scope with m ocks from TP Testing: Robolectric tests

Slide 33

Slide 33 text

Testing 33/40 Application MakeLemonCones
 Activity Toothpick testing offers: • both mockito and easymock support • create mocks with annotations • creates any scope and binds mocks into the scope • create instances and injects on demand your mocks go here

Slide 34

Slide 34 text

Flows and scopes: MVP 34/40 Custom scope Application Activity 1 Activity 1 MVP, keeping the presenter state • Nowadays → Retained fragments, 
 Loaders, etc. • Toothpick → Use a scope to retain 
 the instance of the Presenter

Slide 35

Slide 35 text

Flows and scopes: MVP 35/40 Custom scopes: • are not related to life cycle of objects • can be associated with a scope annotation • an annotated dependency will be created in this custom scope • allow to persist state across rotations • can span across multiple activities (user flows) Custom scope Activity 2 Application Activity 1 Activity 1

Slide 36

Slide 36 text

Flows and scopes: MVP 36/40 Define a depedency Create a custom 
 scope annotation Annotate a 
 dependency to make it a 
 singleton of a 
 custom scope

Slide 37

Slide 37 text

Flows and scopes: MVP 37/40 Presenter
 Scope Application Activity 1

Slide 38

Slide 38 text

Flows and scopes: MVP 38/40 Close the scope
 of the activity as it dies Close the presenter
 scope as you leave this flow App Presenter Activity

Slide 39

Slide 39 text

Flows and scopes: MVP 39/40 User flow scope Activity 3 Application Activity 1 • 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 • The data will be garbage collected as you close the scope when leaving the flow Activity 2

Slide 40

Slide 40 text

40/40 Conclusion • Toothpick is scope tree based DI framework • Fast and simple • Advanced testing support • Offers nice advanced scenarios: 
 Presenter scope, User Flow Scope, etc. Comments // Questions ?