this might be a more generic DI question but tryin...
# kotlin-inject
j
this might be a more generic DI question but trying to find a way to handle this. i have a class that implements two interfaces,
InterfaceA
and
InterfaceB
. i have one object that needs a set of
InterfaceA
implementations and another that needs an implementation of
InterfaceB
. not sure the best way to handle this (code in 🧵 in case this makes no sense)
Copy code
@Component
abstract class AppComponent {
    
    @IntoSet
    @Provides
    fun provideInterfaceA(): ? {
        return MyObject()
    }
    
    @Provides
    fun provideStuff(interfaceAs: Set<InterfaceA>) {
        
    }
    
    @Provides
    fun provideOtherStuff(interfaceB: InterfaceB) {
        ...
    }
    
}

class MyObject : InterfaceA, InterfaceB {
    ...
}
so far the options I can think of are to have 2 provides functions, or have
provideOtherStuff
accept a
Set<InterfaceA>
and then just do
interfaceAs.filterInstanceOf { InterfaceB }
or something
i think the ideal solution would be to have
IntoSet
take a parameter for an object type so I could do
Copy code
@IntoSet(InterfaceA::class)
@Provides
fun provideInterfaceB(): InterfaceB {
    return MyObject()
}
which would create the set binding for
InterfaceA
and the provides method for
InterfaceB
a
This should work:
Copy code
@Component
abstract class AppComponent {
    // Provide the impl
    @Provides
    fun provideMyObject(): MyObject {
        return MyObject()
    }
    
    // Then bind it to whatever interfaces you need
    @IntoSet
    @Provides
    fun provideInterfaceA(myObject: MyObject): InterfaceA {
        return myObject
    }
    
    @Provides
    fun provideInterfaceB(myObject: MyObject): InterfaceB {
        return myObject
    }
    
    @Provides
    fun provideStuff(interfaceAs: Set<InterfaceA>) {
        
    }
    
    @Provides
    fun provideOtherStuff(interfaceB: InterfaceB) {
        ...
    }   
}
j
that requires me to know that
MyObject
inherits from both
InterfaceA
and
interfaceB
though and hard code that. with your solution, i'm not sure how i'd do a test replacement other than making
MyObject
inherit from a third interface so that i can inject that so that i can inject a fake for my tests