Junior — Middle
What is the purpose of using the Binds annotation in Dagger for dependency injection?
sobes.tech AI
Answer from AI
The @Binds annotation in Dagger is used to bind an interface to its implementation without the need to write a factory method manually. It allows specifying that when a dependency of the interface is requested, a specific class implementing that interface should be provided.
Advantages of @Binds:
- Compactness: no need to write the method body, just declare an abstract method.
- Improved readability and maintainability of the code.
- Allows Dagger to generate more efficient code.
Example:
@Module
abstract class NetworkModule {
@Binds
abstract ApiService bindApiService(ApiServiceImpl impl);
}
Here, ApiServiceImpl will be used when injecting ApiService.