Dagger 2 @Binds vs @Provides

Beginning Dagger 2.12 (sorry, I know it's quite some time back.. but still worth understanding it), there’s this new feature called @Binds added to Daggers. There are already some blogs about it. However, reading them still makes me wonder what advantage it has to the old faithful @Provides that we have in our modules.

As my goal is to make things as clear as possible, I’m writing this after making my own investigation on it, in a way that clears my questions.

What’s the different in term of coding

@Provides

@Module
class MyModule {
@Provides
fun getInjectClass(injectObject: InjectClass): InjectInterface {
return injectObject
}

}
class InjectClass @Inject constructor(): InjectInterface
interface InjectInterface

@Binds

@Module
abstract class MyModule {
@Binds
abstract fun getInjectClass(injectObject: InjectClass):
InjectInterface
}
class InjectClass @Inject constructor(): InjectInterface
interface InjectInterface

From the above, there’s no obvious clear advantage of @Binds over @Provides. One got turned into a abstract class and function, while the other is a concrete one. It doesn’t reduce any verbosity (or boilerplate).

--

--