https://kotlinlang.org logo
#kotlin-inject
Title
# kotlin-inject
m

Marco Pierucci

03/03/2024, 9:30 PM
What's the recommended approach to binding and internal implementation to a interface, So that module A consumes Module B can expose an interface from B without needing to expose its impl?
e

evant

03/03/2024, 11:01 PM
More concrete examples would be helpful but the handwavy answer is parent components can be an interface with the impl living somewhere else, you just need to provide methods for the types you are using
m

Marco Pierucci

03/03/2024, 11:21 PM
Ok I'll try to use a concrete example:
Copy code
Module A

interface ApplicationComponent
( The implementation live on different sourcets but thats not important I believe
Copy code
Module B

interface Repository

internal class RepositoryImpl(more internal deps): Repository

interface ModuleBComponent {
   @Provides 
   fun provideRepo(impl:RepositoryImpl): Repository = iml
    (This obcs does nto compile
}
The usual pattern I go with is :
Copy code
AplicationComponent: ModuleBComponent
Ive tried a fiew works around with abstract classes bu eventually ModuleA generated code tries to reference the internal class so compilation is not possible
e

evant

03/03/2024, 11:41 PM
right, if you think about it, implementing ModuleBComponent requires referencing RepositoryImpl. One thing you can do is
Copy code
interface ModuleBComponent {
    val repo: Repository
}

@Component
abstract class MyoduleBComponentImpl : ModuleBComponent {
    @Provides 
   fun provideRepo(impl:RepositoryImpl): Repository = impl
}

...

@Component
abstract class ApplicationComponent(@Component val moduleB: ModuleBComponent)
with the downside is you'll need different scopes for the 2 components, which may or may not be fine depending on how circular your dependencies are
m

Marco Pierucci

03/03/2024, 11:42 PM
Interesting, will deffo give it a try at this tomorrow, cheers!
e

evant

03/03/2024, 11:43 PM
long-term I do want to handle restricted visibility better, but it's tricky because you have to generate bridge code to call into non-public methods
🙌 1
👀 1
m

Marco Pierucci

03/04/2024, 12:12 AM
Yeah soudns about right. Thanks for all the effort btw!
3 Views