Dagger Flashcards
learn dagger
What is the purpose of the @Component annotation?
Provides dependencies app wide.
How can dependencies be returned using @Component?
By defining methods like fun getUserPreferences(): SharedPref.
What does the @Inject annotation signify?
It indicates constructor or field injection.
What is constructor injection in Dagger?
SomeClass @Inject constructor().
What is field injection in Dagger?
lateinit var repo: StoreRepository.
What is the role of @Module and @Provides?
Defines how to provide objects for interfaces or classes that Dagger cannot instantiate.
What is an example of a @Module class?
@Module class NetworkModule { @Provides fun getFeedAPIs(): getFeedAPIs { … } }
When should @Binds be used?
When creating an object of an interface that can be instantiated without additional effort.
What is the difference between @Provides and @Binds?
@Provides is for complex creation, @Binds is for simple constructor-based creation.
What type of class must @Provides be in?
A class.
What type of class must @Binds be in?
An abstract class with abstract functions.
What is the purpose of the @Named annotation?
To resolve ambiguity when multiple implementations of an interface are available.
How is @Named used in a Dagger module?
@Named(“mock”) @Binds abstract fun getUserRepo(mockRepo: MockRepo): UserRepo.
What is a Qualifier annotation in Dagger?
A type-safe way to distinguish between different implementations.
What is the implementation of @Singleton?
@Scope @Documented @Retention(RUNTIME) public @interface Singleton {}
What does the @Singleton scope imply?
Objects marked with @Singleton will have the same instance within a component.
How do you handle dynamic/runtime dependencies in Dagger?
Pass them to a module and set them during component creation.
What is a Factory method in Dagger?
A method that ensures runtime dependencies are set during component creation.
Fill in the blank: The @Component.Factory interface allows for _______.
creating components with runtime dependencies.
True or False: @Binds requires an implementation.
False.
What happens if multiple variables of the same type are marked with @Singleton?
They will share the same object within the component.
What is the risk of not setting a module with a runtime dependency?
Code compiles but breaks at runtime.
@Component
Provides dependencies app wide
Can return a dependency or have dependency consumers.
What is the purpose of @Inject?
Used for constructor injection or field injection.
@Module & @Provides
Define how to provide objects for interfaces or classes Dagger cannot instantiate on its own.
@Binds
Used when creating an object of an interface that can be instantiated without extra effort.
What is the difference between @Provides and @Binds?
@Provides is used for complex object creation; @Binds is for simple constructor-based creation.
What does @Named do?
Specifies which implementation of an interface to use when multiple implementations exist.
What is the purpose of @Qualifier?
Provides a type-safe alternative to @Named to avoid typos and ensure correct implementation is used.
Dynamic/Runtime Dependency involves passing what to the Module?
Dynamic dependencies that must be set while creating the component.
@Singleton
Defines a scope for objects to ensure only one instance exists within the component’s lifecycle.