I have a data class that must be shared between al...
# koin
p
I have a data class that must be shared between all the app. That dataclass value is get in a viewmodel, using a corutine, etc... How can I share that between the app with Koin? I found that you can change a Koin single value doing this: getKoin().setProperty("ProjectConfig", newValue). I don't know if it works, I didn't test it. Before I wanna know if this is a good practice or not. Or maybe alternatives.
w
This is not a Koin problem but an architecture problem. If you need to expose data that can change but you want to maintain a single instance of the holder of that data, use a service
p
I'm not sure I know what you mean, can you share a simple demo or link?
w
I don’t. What you need is a class that can be updated and provide updates. data class doesn’t fit
So generally you would reach for a Service.
Or you would wrap your network calls in a service that can rebroadcast past values
You could also just keep your ViewModel in Koin and fetch the value out wherever needed
p
thank you but I'm not sure about these approaches
r
I am not a guru, but hoping the below might help. I think it helps to have a layer under your repo, a view model or service as Matt mentioned. This is my pattern example below in my application class. One thing to look out for is not have one service or viewmodel use another, circular reference risk and can be hard to debug.
Copy code
val appModule = module {
    // Strict flow = Routes → Services → Repositories → Data Sources
    singleOf(::ItemRepositoryImpl) { bind<ItemRepository>() }
    singleOf(::ItemService)
    singleOf(::InventoryService)
    ...
    }
Then I can call the singleton service (or viewmodel) from my route (or view or other places as needed).
Copy code
fun Route.itemRoute(
    itemService: ItemService,
    inventoryService: InventoryService,
    costService: CostService,
) {
Regards,