I have created a custom component AuthUserComponent
using Hilt and need to provide multiple implementation to DataRepository
interface.
class Sample @Inject constructor(
@DemoMode private val demoRepository: DataRepository,
@ProductionMode private val productionRepository: DataRepository
) {}
I have created the below @Provides implementations of the interface:
Module
@InstallIn(AuthUserComponent::class)
object DIModule {
@AuthUserScope
@DemoMode
@Provides
fun provideDataRepositoryImplDemo(): DataRepository =
DataRepositoryImplDemo()
@AuthUserScope
@Provides
@ProductionMode
fun provideDataRepositoryImpl(): DataRepository =
DataRepositoryImpl()
}
How do I provide multiple repository implementations via Entrypoint and bridge it with SingletonComponent? I get the below error:
DataRepository is bound multiple times error
@InstallIn(AuthUserComponent::class)
@EntryPoint
interface AuthUserDataEntryPoint {
@ProductionMode
fun dataRepositoryImpl(): DataRepository
@DemoMode
fun dataRepositoryImplDemo(): DataRepository
}
@Module
@InstallIn(SingletonComponent::class)
internal object AuthUserDataEntryBridge {
@DemoMode
@Provides
internal fun provideDataRepositoryImplDemo(
authUserComponentManager: AuthUserComponentManager
): DataRepository {
return EntryPoints
.get(authUserComponentManager, AuthUserDataEntryPoint::class.java)
.dataRepositoryImplDemo()
}
@ProductionMode
@Provides
internal fun provideDataRepositoryImpl(
authUserComponentManager: AuthUserComponentManager
): DataRepository {
return EntryPoints
.get(authUserComponentManager, AuthUserDataEntryPoint::class.java)
.dataRepositoryImpl()
}
}
Have you considered making a wrapper object that gets injected with the annotated properties and then you get that object using EntryPoints?
data class DataRepositoryies @Inject constructor(
@DemoMode
val demoRepository: DataRepository,
@ProductionMode
val productionRepository: DataRepository
)
And then somewhere you'd do
val dataRepositories = EntryPoints.get(..., DataRepositories::class.java)
val demoRepository = dataRepositories.demoRepository
val productionRepository = dataRepositories.productionRepository
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.