I have a setup something like this:
interface Animal {
}
class Dog @Inject constructor() : Animal
class AnimalProxy @Inject constructor(
val animalFactory: AnimalFactory,
val animalMap: Map<AnimalType, Animal>
) : Animal
enum class AnimalType {
Pet,
Wild
}
class AnimalFactory @Inject constructor ()
And this is how I am binding these object in the module
@Module
@InstallIn(SingletonComponent::class)
class AnimalModule {
@MapKey
annotation class AnimalTypeKey(val value: AnimalType)
@Named(DOG)
@Provides
fun provideDog(
): Animal {
return Dog()
}
@Provides
@Singleton
@IntoMap
@AnimalTypeKey(AnimalType.Pet)
@Named(PROXY)
fun provideAnimalProxy(
animalProxy: AnimalProxy
) : Animal = animalProxy
companion object {
const val DOG = "dog"
const val PROXY = "proxy"
}
}
But somehow something is not quite right and I am not able to figure out what's going on. I get an error cannot be provided without an @provides-annotated method
. I know something is wrong when I am creating provideAnimalProxy
but can't figure it out. Other working option I have is:
@InstallIn(SingletonComponent::class)
@Module
class AnimalsModule {
@Singleton
@Provides
fun provideProxy(
): Animal {
return AnimalProxy(
AnimalFactory(),
mapOf(
AnimalType.Pet to Dog(),
),
)
}
}
But this one feels redundant as for AnimalFactory
I already have an inject
constructor.