Closed matejdro closed 4 weeks ago
AppComponent
will implement the Factory
interface. You cannot inject components themselves, therefore you cannot inject Factory
. Simply take the AppComponent
and call the createRendererComponent()
function on it.
That kinda breaks the non-centralised nature of Anvil, since you still need access to the component instance to create subcomponents.
Is this a fault of kotlin-inject-anvil or kotlin-inject?
kotlin-inject, but it's the same in Dagger. You need the reference to the component for the parent-child relationship of the components. There's no way around it. Think of the @Factory
annotation the same way as a @ContributesTo
annotation.
Doh, the solution is just to add a @Provides
function to your main component, that points to itself, which allows injection of it anywhere.
@Provides
fun provideRendererSubcomponentFactory(): RendererComponent.Factory = this
do you think it would be a good idea to add this to the README? I can make a PR if necessary.
but it's the same in Dagger
I'm pretty sure that with Dagger's Anvil, you can directly inject subcomponent factories.
To be honest, I never tried this 🙃 Instead of updating the README, this function should be generated.
Thank you!
Maybe a bit stupid question, but I've been trying to figure out how to actually inject the factory into something to instantiate the subcomponent:
But above fails with
Cannot find an @Inject constructor or provider for: RendererComponent.Factory