user2141889
user2141889

Reputation: 2305

Dagger 2 Component.Builder

I have been wondering what are the benefits of creating own @Component.Builder inside Components instead of using default ones? The documentation does not say much about them, nor I could have found any reasonable examples. Anyone could share some thoughts?

Upvotes: 5

Views: 1045

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95634

A few of the advantages:

  • As Jeremy pointed out in the comments, you'll need an explicit interface if you want to use @BindsInstance.
  • An explicit interface lets you name your Module methods arbitrarily or add per-method Javadoc to your builder methods. This might be especially handy if your Module instances are optional or if they need to be manually created.
  • Some IDEs and tools don't do well with code-generated interfaces. An explicit Builder lets you define your own tool-readable interfaces and let Dagger generate the implementations later.
  • An explicit interface may make it easier to mock your component builders in unit tests. This may be especially handy for subcomponent builders, which observe the same rules as component builders.

Upvotes: 6

Related Questions