https://kotlinlang.org logo
#koin
Title
# koin
s

s3rius

09/20/2023, 6:51 PM
Hey, I have a sort-of general question about how you would solve the issue of your dependencies not aligning entirely. I'll post the details as a reply because it's a couple of lines.
Let's take this example: I have a
HttpClient
that I want to inject a
serverUrl
into. The url is part of another class (which is available as a dependency). To visualize it:
Copy code
class HttpClient(private val serverUrl: String) { } // want to create this

data class AppInfo(
	val serverUrl: String // need this as dependency
	val platform: String
)
As I see it, I have a few options. All these options would work but I'm looking for a conceptually sound solution. Option 1: adjust the HttpClient. Just change the
HttpClient
to take
AppInfo
instead. But now I'm passing significantly more into my client as I need, and I might be creating dependencies that I don't want to create.
Copy code
class HttpClient(private val appInfo: AppInfo) { }
Option 2: provide the correct data in the module declaration This is a simple solution but turns the module into a kind of garbage bin if these things happen more often.
Copy code
single { HttpClient(get().serverUrl) }
Option 3: create a factory class Put a factory class (or factory function) somewhere and use that factory. That seems like an okay solution, but it's more convoluted.
Copy code
class HttpFactory(appInfo: AppInfo) {
	fun create() = HttpClient(appInfo.serverUrl)
}

single { HttpFactory(get()).create() }
Option 4: make everything a dependency For a few values like
serverUrl
it sounds reasonable. But this totally isn't scalable and stops being reasonable when you consider other scenarios.
Copy code
single(tag = "platform") { get<AppInfo>().platform }
single(tag = "serverUrl") { get<AppInfo>().serverUrl }
So to summarize, I think I just don't know who is actually responsible for this work, so I don't know where to put the code.
p

psh

09/21/2023, 3:57 PM
I've previously gone with a minor tweak of Option 1 - your AppInfo class could implement an interface and the declared dependency of the HttpClient is the interface and not exposing the whole object.