Hi, there. Is there any good tips for connecting t...
# koin
s
Hi, there. Is there any good tips for connecting two koin instances? My application contains many feature gradle modules. And I want to segregate each feature module’s own koin modules. Easies way to do this is to create each module’s own Koin instances. But I want to provide some shared dependencies among them. ( like instance of common modules)
Copy code
Feature Module A: create Koin for A
Feature Module B: create Koin for B
Common Feature Module: create Koin for Common. But Koin A, Koin B cannot get instances from Common Koin
Another way is each module define it’s own scope and define their own dependencies in there scope. By this way, they can use instances of shared library module and segregate their own dependencies. But to force feature module to use scope is not convenient. I should put scope before every
get()
,
inject()
call in modules.
Copy code
Common Feature Module: create Koin
Feature Module A: create scopeA inside Common Koin
Feature Module B: create scopeB inside Common Koin
What I want is introducing parent - child Koin. In this concept, I can use
KoinComponent
easily and can segregate each feature module.
Copy code
Common Feature Module: create Koin for Common
Feature Module A: create Koin for A. set Koin for Common as parent Koin
Feature Module B: create Koin for B. set Koin for Common as parent Koin
Is this reasonable feature? Or is there any other better solution for this?
k
Why would you like to segregate each feature into their own koins? We're having 50+ gradle modules, each with their own
koin module
, and whole graph is built combining into single koin instance in
App
module. Works without issues for us.. and the overhead is not that big, since instances are created lazily
s
@kzotin Thank you for sharing your experience. In my project, many teams make their own features in each team’s feature module. So they don’t care other team’s module and it is encouraged thing. I’m managing shared module, so only I care about whole modules. Single Koin instance works well in most cases, but when multiple modules define object with same type, Koin crashes with duplicated definition error. That’s is natural thing. To avoid this issues, each team should define these duplicated defined module with qualifier. But it is not easy to guess which object other team uses. That’s my major issue. I don’t want to make each team care about what other team define in their koin module definition. To do this, some kind of segregation ( scope / koin / etc ) is needed.
k
Could you provide few examples of such objects with the same type? You may agree to have own classes for each feature (e.g.
ItemsStorage: Storage<Item>
)
s
@kzotin Some classes from 3rd party libraries. ex) Retrofit, Gson, Picaso Yeah, I know that that kind of class instances does not need be defined each feature modules. But in my team, each team prefer to use their own classes.