Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Last active June 26, 2024 10:57
// For example, we have an interface called LoggerSource.
// It has two separate implementations:
// InMemoryLogger & DatabaseLogger
@Qualifier
annotation class InMemoryLogger
@Qualifier
annotation class DatabaseLogger
// When creating the `LoggerModule`, we can define
// two abstract methods for both the implementations
// and add the appropriate @qualifiers for each method.
@InstallIn(SingletonComponent::class)
@Module
abstract class LoggerModule {
@DatabaseLogger
@Binds
abstract fun bindDatabaseLogger(impl: LoggerLocalDataSource): LoggerSource
@InMemoryLogger
@Binds
abstract fun bindInMemoryLogger(impl: LoggerInMemoryDataSource): LoggerSource
}
// When injecting the interface in MainActivity,
// we can specify which implementation we want to inject
// using the qualifier name
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@DatabaseLogger
@Inject
lateinit var logger: LoggerSource
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment