Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Toothpick - A fresh approach to DI - Short Version

Toothpick - A fresh approach to DI - Short Version

Presented at GDG East Bay - Samsung office

C716975ef0b4ed35553572ff44ea1bed?s=128

Daniel Molinero Reguera

September 26, 2017
Tweet

Transcript

  1. A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/

  2. 5IF"OESPJE5FBNJT)JSJOH
 jobs.groupon.com/careers/ @D_Lemures #ToothpickDI @GrouponEng @SteffNicolas

  3. THE TALK WHY TOOTHPICK? USING TOOTHPICK 
 REUSING INSTANCES 


    
 VERSION 2.0 CONCLUSION DEFINING DEPENDENCIES INJECTING DEPENDENCIES MODULES & BINDINGS SCOPE TREE SCOPE SINGLETONS MULTI-ACTIVITY FLOW @D_Lemures | #ToothpickDI
  4. WHY TOOTHPICK? @D_Lemures | #ToothpickDI The Ancient Roma Coffee Machine

    Visual Toy javierarres.wordpress.com
  5. WHY TOOTHPICK? SIMPLER BETTER TEST SUPPORT EVEN FASTER @D_Lemures |

    #ToothpickDI
  6. USING TOOTHPICK public class DealPresenter {
 
 @Inject DealApiClient apiClient;

    @Inject Navigator navigator; 
 } @Singleton
 public class DealApiClient {
 Retrofit retrofit;
 
 @Inject
 public IceMachine(Retrofit retrofit) {
 this.retrofit = retrofit;
 }
 } JSR 330 annotations→nothing new here @D_Lemures | #ToothpickDI DEFINING DEPENDENCIES
  7. INJECTING DEPENDENCIES public class DealActivity extends Activity { @Inject DealPresenter

    presenter;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(getApplication()); Toothpick.inject(this, appScope);
 } } @D_Lemures | #ToothpickDI USING TOOTHPICK
  8. INJECTING DEPENDENCIES @D_Lemures | #ToothpickDI USING TOOTHPICK SCOPE MAKE INJECTIONS

    public class DealActivity extends Activity { @Inject DealPresenter presenter;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(getApplication()); Toothpick.inject(this, appScope);
 } }
  9. INJECTING DEPENDENCIES @D_Lemures | #ToothpickDI USING TOOTHPICK DEAL PRESENTER public

    class DealActivity extends Activity { @Inject DealPresenter presenter;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(getApplication()); Toothpick.inject(this, appScope);
 } } DEAL APICLIENT NAVIGATOR RETROFIT
  10. public class DealActivity extends Activity { @Inject DealPresenter presenter;
 


    @Override protected void onCreate() {
 super.onCreate();
 
 Scope appScope = Toothpick.openScope(getApplication()); appScope.installModules(new Module() {{ bind(Navigator.class).to(NavigatorImpl.class);
 bind(Retrofit.class).toInstance(retrofitInstance); }}); Toothpick.inject(this, appScope);
 } } MODULES & BINDINGS @D_Lemures | #ToothpickDI USING TOOTHPICK BINDINGS MODULES SCOPE
  11. @D_Lemures | #ToothpickDI USING TOOTHPICK SCOPE 1 SCOPE 2 SCOPE

    4 SCOPE 3 MODULES & BINDINGS
  12. APPLICATION
 SCOPE ACTIVITY 1
 SCOPE ACTIVITY 2
 SCOPE SERVICE 1


    SCOPE FRAGMENT 3
 SCOPE FRAGMENT 1
 SCOPE FRAGMENT 2
 SCOPE SCOPE TREE @D_Lemures | #ToothpickDI USING TOOTHPICK
  13. public class DealActivity extends Activity { @Inject DealPresenter presenter; @Inject

    Context context;
 
 @Override protected void onCreate() {
 super.onCreate();
 
 Scope scope = Toothpick.openScope(getApplication(), this); scope.installModules(new Module() {{
 bind(Context.class).toInstance(this); }}); Toothpick.inject(this, scope);
 } } SCOPE TREE APPLICATION
 SCOPE ACTIVITY
 SCOPE @D_Lemures | #ToothpickDI USING TOOTHPICK
  14. SCOPE SINGLETONS @D_Lemures | #ToothpickDI REUSING INSTANCES ANDROID INTENT ACTIVITY

    FRAGMENT RECYCLER VIEW LOCAL SINGLETON
  15. APPLICATION
 SCOPE ACTIVITY
 SCOPE APP SINGLETONS ACTIVITY SINGLETONS SCOPE SINGLETONS

    @D_Lemures | #ToothpickDI REUSING INSTANCES
  16. public class Module extends Module {
 public Module() {
 bind(ViewHelper.class).toInstance(new

    ViewHelper());
 }
 } SCOPE SINGLETONS SINGLETONS CAN BE DEFINED IN MODULES @D_Lemures | #ToothpickDI REUSING INSTANCES
  17. @Singleton
 public class DealApiClient {
 } @ActivitySingleton
 public class ViewHelper

    {
 } APPLICATION
 SCOPE ACTIVITY
 SCOPE SCOPE SINGLETONS OR USING ANNOTATIONS @D_Lemures | #ToothpickDI REUSING INSTANCES
  18. ACTIVITY 1 ACTIVITY 3 ACTIVITY 2 MULTI ACTIVITY FLOWS Purchase

    Flow @D_Lemures | #ToothpickDI REUSING INSTANCES
  19. APPLICATION SCOPE ACTIVITY 1 SCOPE FLOW SCOPE ACTIVITY 3 SCOPE

    ACTIVITY 2 SCOPE MULTI ACTIVITY FLOWS @D_Lemures | #ToothpickDI REUSING INSTANCES
  20. public class MyActivity extends Activity {
 
 @Inject ShoppingCart shoppingCart;

    
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Scope scope = openScopes(getApplication(), 
 FlowSingleton.class, 
 this);
 Toothpick.inject(this, scope);
 } } @D_Lemures | #ToothpickDI REUSING INSTANCES MULTI ACTIVITY FLOWS
  21. @FlowSingleton
 public class ShoppingCart {
 List<PurchaseItem> purchases…
 } FLOW SCOPE

    MULTI ACTIVITY FLOWS @D_Lemures | #ToothpickDI REUSING INSTANCES
  22. VERSION 2.0 RELEASABLE SINGLETONS @D_Lemures | #ToothpickDI BETTER SCOPE ANNOTATIONS

    KILLING REGISTRIES BETTER API
  23. CONCLUSION QUESTIONS ? COMMENTS ? @D_Lemures | #ToothpickDI

  24. A FRESH APPROACH TO DI TOOTHPICK https://github.com/stephanenicolas/toothpick/ @D_Lemures #ToothpickDI @GrouponEng

    @SteffNicolas