s3rius
09/20/2023, 6:51 PMHttpClient
that I want to inject a serverUrl
into. The url is part of another class (which is available as a dependency). To visualize it:
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.
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.
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.
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.
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.psh
09/21/2023, 3:57 PM