java - Dagger 2 - what's the proper way to use the components and modules? -


after playing around dagger 2, managed inject dependencies dependent objects, not sure right way of doing it, , want proper way.

the way did it, have one component includes multiple modules , injection in top class, , dependencies "seep" in. proper way?

app component:

@singleton @component(modules = {appmodule.class, servicemodule.class, callalarmmodule.class}) public interface appcomponent {     void inject(healthcheckerapplication app);     void inject(settingsactivity activity);     void inject(alertcreator alertcreator);     void inject(healthservice service);     inotificationdatafactory providefactory(); } 

modules:

@module public class appmodule {     healthcheckerapplication app;     alertcreator alertcreator;      public appmodule(healthcheckerapplication app) {         this.app = app;         this.alertcreator = new alertcreator(app);     }      @provides     @singleton     public sharedpreferences providesharedprefs() {         return preferencemanager.getdefaultsharedpreferences(app);     }      @singleton     @provides     public alertcreator providealertcreator() {         return alertcreator;     } }  @module public class servicemodule {      healthservice service;      public servicemodule(healthservice service) {         this.service = service;     }      @singleton     @provides     public healthservice provideservice() {         return service;     }      @provides     public inotificationdatafactory providefactory() {         return new notificationdatafactory();     }  }  @module public class callalarmmodule {      iminutescounter.itimercallback timercallback;      public callalarmmodule(iminutescounter.itimercallback timercallback) {         this.timercallback = timercallback;     }      @singleton     @provides     public iminutescounter.itimercallback providecallalarm() {         return timercallback;     }      @singleton     @provides     public iminutescounter provideminutesmonitor() {         return new minutesmonitor(timercallback);     } } 

using in code:

healthservice:

public class healthservice extends service {      @inject     public notificationcreator notificationcreator;      @inject     public calltoolongalarm calltoolongalarm;      @override     public void oncreate() {         super.oncreate();         // creating modules here (besides appmodule) , let dependencies "seep" in         appcomponent appcomponent = ((healthcheckerapplication) getapplication()).getappbuilder().servicemodule(new servicemodule(this)).callalarmmodule(new callalarmmodule(calltoolongalarm)).build();         appcomponent.inject(this);          notificationdatafactory = appcomponent.providefactory();     } } 

application:

public class healthcheckerapplication extends application {     daggerappcomponent.builder appbuilder;      @override     public void oncreate() {         super.oncreate();         appbuilder = daggerappcomponent.builder()                 .appmodule(new appmodule(this));     }      public daggerappcomponent.builder getappbuilder() {         return appbuilder;     } } 

calltoolongalarm:

public class calltoolongalarm implements minutesmonitor.itimercallback {      private healthservice healthservicecontext;      @inject     public iminutescounter minutesmonitor;      @inject     public calltoolongalarm(healthservice context) {         this.healthservicecontext = context;     } } 

is how dagger 2 should used? keep in mind access alertcreator in multiple places, needed make sure same instance in places, created in appmodule.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -