Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 457
user2141889 Avatar asked Mar 04 '17 23:03

user2141889


People also ask

Is dagger 2 deprecated?

It's officially deprecated and you can pretty much ignore it. Google's framework, which became dominant in Android ecosystem, was originally called Dagger 2. Sometimes we still refer to it as such, but, in most cases, we simply call it Dagger today.

What is @component in dagger?

This is called a Dagger component; it contains a graph that consists of the objects that Dagger knows how to provide and their respective dependencies. Kotlin Java. // @Component makes Dagger create a graph of dependencies.

Can we create custom scope in Dagger 2?

create custom scopes with Dagger is pretty easy, you just have to follow these steps. Step 1) declare your annotation. Step 2) annotate the dependencies with this annotation in the module.

What is dagger 2 used for?

Dagger 2 relies purely on using Java annotation processors and compile-time checks to analyze and verify dependencies. It is considered to be one of the most efficient dependency injection frameworks built to date.


1 Answers

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.
like image 151
Jeff Bowman Avatar answered Sep 24 '22 15:09

Jeff Bowman