evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.29k stars 60 forks source link

Is nesting @Assisted possible? #407

Open ursusursus opened 5 months ago

ursusursus commented 5 months ago

Hi, I'm migrating from dagger and noticed this library supports assisted injection natively, so I'm triying it out (I didn't use it in dagger because of the extra dependency).

At UI layer I have conductor Controller (Fragment equivalent), which has all the lifecycle callbacks & functions + it implements navigation for me

class ChatViewModel(
    private val dispatcherProvider: DispatcherProvider,
    private val intentHelper: IntentHelper,
    private val navigator: ChatNavigator
)

class ChatController : conductor.Controller, ChatNavigator

interface ChatNavigator {
    fun goToFoo()
}

class IntentHelper(private val controller: Controller)

My current solution for this is to have a manual Factory

class ChatViewModelFactory @Inject constructor(
    private val dispatcherProvider: DispatcherProvider,
) {
    fun create(controller: ChatController) {
        return ChatViewModel(
            dispatcherProvider = dispatcherProvider,
            intentHelper = IntentHelper(controller),
            navigator = controller
        )
    }
}

Is this something I can do away with using @Assisted? Simple example obviously works

@Inject
class ChatViewModel(
    private val dispatcherProvider: DispatcherProvider,
    @Assisted private val navigator: ChatNavigator
)

But adding the IntentHelper causes me trouble

@Inject
class ChatViewModel(
    private val dispatcherProvider: DispatcherProvider,
    private val intentHelper: IntentHelper, <------------
    @Assisted private val navigator: ChatNavigator
)

@Inject
class IntentHelper(@Assisted private val controller: Controller) <------

I'm trying to have it be part of the graph & have the controller asgument be assisted, but that then obviously requires the view model to depend on the factory private val intentHelper: (Controller) -> IntentHelper,, not the type itself

So .. is this even possible, or am I stuck with manual factories? (to not have Controller dependency explicitly in viewmodel constructor signature)

evant commented 5 months ago

I'm not sure I'm following your question, how would you expect to pass the controller arg?