Last active
June 26, 2024 10:57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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