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
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
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
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
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
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
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
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
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
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 !
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)
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…