What is the difference between addTo and include in Java Dagger?

I'm not sure what the difference is? When should I use this?

http://square.github.io/dagger/javadoc/index.html

+3


source to share


1 answer


includes

indicates which module the current module belongs to. For example, this is useful for aggregating all of your modules statically:

@Module(
  includes = { AndroidModule.class, NetworkModule.class, StorageModule.class }
)
public class RootModule() {
}

// other file
objectGraph = ObjectGraph.create(new RootModule());

      

Instead of dynamic:

objectGraph = ObjectGraph.create(
     new AndroidModule(), 
     new NetworkModule(), 
     new StorageModule());

      



So making full use of compile-time graph validation.

addsTo

refers specifically to the relationship between parent and child modules. It indicates that the module is an extension of some module and is used as a parameter .plus()

. For example. with two modules:

@Module(
  //...
)
public class ParentModule() {
  //...
}

@Module(
  addsTo = { ParentModule.class },
  //...
)
public class ChildModule () {
  //...
}

      

this configuration means that parentGraph = ObjectGraph.create(new ParentModule());

you can then execute childGraph = parentGraph.plus(new ChildModule());

somewhere in your code to create an extended, usually short-lived child graph.

+9


source







All Articles