https://kotlinlang.org logo
Title
j

Jebus_Chris

01/21/2023, 3:19 PM
What makes more sense regarding dependency injection? Making the object and passing it on, or passing the parameters so the dependent service can make the object? In this example I’m making a service that relies on an external api (not limited to that, could be a database for example). The url and token would come from a config file
private val apiClient = APIClient(apiUrl, apiToken)
    private val someService(apiClient)         // option 1
    private val someService(apiUrl, apiToken). // option 2
r

Rob Elliot

01/21/2023, 4:22 PM
I like option 1, as it lets me inject a different implementation of
APIClient
. You can always provide both.