-
Notifications
You must be signed in to change notification settings - Fork 0
Dagger β Named annotation and qualifiers
Devrath edited this page Oct 6, 2023
·
7 revisions
Contents |
---|
Observations |
Output |
Code |
- The
named annotations
orqualifiers
are used to differentiate between two different values in a single class type that is being injected using the dagger. - One such scenario is say you have an interface and 2 different implementations of the interface, Now you create 2 modules for this interface and then add both the modules in the component you are preparing. This would result in an error since
dagger
doesn't understand which implementation to use. - We can resolve this by specifying a qualifier for this purpose.
OS type is Android
Contents |
---|
Implementations |
Qualifiers |
Modules |
Components |
Activity |
OS.kt
interface OS {
fun getOsType()
}
AndroidOs.kt
class AndroidOs @Inject constructor() : OS {
override fun getOsType() {
printLog("OS type is Android")
}
}
AppleOs.kt
class AppleOs @Inject constructor() : OS {
override fun getOsType() {
PrintUtils.printLog("OS type is IOS")
}
}