Slide 1

Slide 1 text

Migrate to TP form RG 4.. https://github.com/stephanenicolas/toothpick/wiki

Slide 2

Slide 2 text

Toothpick is a scope tree based Dependency Injection (DI) library for Java. It is a full-featured, runtime based, 
 but reflection free, implementation of JSR 330. Toothpick is faster than Dagger 1 or Dagger 2
 for a relatively small number of injections (<1K) 2

Slide 3

Slide 3 text

Presentation plan 1. TP  scopes   2. Injec/ng  all  dependencies  into  an  object   3. Ge9ng  an  instance/lazy/provider  of  a  class   4. Named  injec/ons     5. Lazies     6. ContextSingleton  and  why  you  should  not  use  it     7. Tes/ng  &  Mock Migrate to TP

Slide 4

Slide 4 text

TP scopes In  Toothpick,  scopes  are:     • a  place  to  define  bindings  (via  modules)   • a  place  to  recycle  injected  instances   Scopes  are  used  to:     • inject  dependencies  into  instances   • getInstance  of  classes Migrate to TP

Slide 5

Slide 5 text

TP scope tree It’s a common practice to define a scope by context. In advanced uses cases, more scopes can be created. Scopes form a tree. Migrate to TP

Slide 6

Slide 6 text

Before: RG.getInjector TP scopes are similar to injector in RG.
 In MGA, this is managed by the foundations classes. Migrate to TP

Slide 7

Slide 7 text

openScope(s) methods need “the name” of the scope as a parameter. 
 If multiple scopes are open together, they form a hierarchy 
 and the leaf is returned. Opening and closing scopes in TP Migrate to TP

Slide 8

Slide 8 text

Opening and closing scopes in TP Migrate to TP

Slide 9

Slide 9 text

Installing modules in TP Modules are very similar in RG and TP. 
 The main difference is that TP doesn’t need a configure method, you can declare bindings directly in the constructor of the modules. Migrate to TP

Slide 10

Slide 10 text

Injecting dependencies: RG 4 Migrate to TP

Slide 11

Slide 11 text

Injecting dependencies: TP Migrate to TP

Slide 12

Slide 12 text

Getting instance of classes: RG 4 This was equivalent to declare a field 
 @Inject ImageCacheWarmup imageCacheWarmup. RG would return a new instance or recycle a previous instance in the scope of the injector, depending on annotations of ImageCacheWarmup. Migrate to TP

Slide 13

Slide 13 text

Getting instance of classes: TP Still equivalent to declare a field 
 @Inject ImageCacheWarmup imageCacheWarmup. TP will also return a new instance or recycle a previous instance in the scope of the injector, depending on annotations of ImageCacheWarmup. Migrate to TP

Slide 14

Slide 14 text

Getting lazies/providers of classes: TP TP API also allows to easily get a provider or a lazy on a dependency. This is also equivalent to declaring a lazy / provider field. Migrate to TP

Slide 15

Slide 15 text

Named injections Migrate to TP Named injections are simpler with TP. You don’t need to use Named. Before with RG After with TP

Slide 16

Slide 16 text

Lazies: RG Migrate to TP You had to declared AND initialize Lazies with RG. 
 But it was pure boiler plate code.

Slide 17

Slide 17 text

Lazies: TP Migrate to TP !!! TP makes Lazies simpler, just declare Lazies. Don’t forget to @Inject them !!!

Slide 18

Slide 18 text

Avoid ContextSingleton Migrate to TP ContextSingleton is just NOT clear. 
 You can create a singleton at the app level or at the activity level, depending on where the injection first happens. This easily creates leaks !

Slide 19

Slide 19 text

Use Singleton or ActivitySingleton Migrate to TP Try your best to either use a real app singleton (and only use the application there) 
 OR an ActivitySingleton (and use the activity as a context)

Slide 20

Slide 20 text

Testing and mocking: RG Migrate to TP You can inject test dependencies or mock the dependencies needed under test…

Slide 21

Slide 21 text

Testing and mocking: TP Migrate to TP Nothing changed (for now). But you don’t need to reset the mocks anymore (weird cases are solved).

Slide 22

Slide 22 text

Conclusion Migrate to TP TP  is  just:  beVer,  easier,  faster  than  RG.     Dig into the TP wiki to get more insight on TP. TP   has   more   to   offer   than   RG:   scopes   spanning   mul/ple   ac/vi/es,   state   preserva/on  across  rota/ons,  etc.  We  will  slowly  use  these  features  in  the  app   over  next  releases…


Slide 23

Slide 23 text

Questions ? Migrate to TP Comments ?