stephanenicolas / toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Apache License 2.0
1.12k stars 115 forks source link

Bind classes which depends on each other in a subscope [QUESTION] #419

Open imrepapp opened 4 years ago

imrepapp commented 4 years ago

Hi,

when I try to add two bindings in a subscope which depends on each other the injection does not work. (it works if I add them to the rootScope)

What did I miss?

Thanks

KTP.openRootScope()
    .openSubScope(SUB_SCOPE) //if remove this line it is working
    .installModules(
        module {
            bind<Foo>().toProvider(FooProvider::class)

            bind<Bar>().toProvider(BarProvider::class)
        }
    )
    .inject(this)

interface Foo {
    val text: String
}

@Singleton
@ProvidesSingletonInScope
//@InjectConstructor //didn't help
class FooProvider: Provider<Foo> {
    override fun get() = object : Foo {
        override val text: String = "Foo"
    }
}

interface Bar {
    val fooInstance: Foo
}

@Singleton
@ProvidesSingletonInScope
@InjectConstructor
class BarProvider(
    val injectedFoo: Foo
): Provider<Bar> {
    override fun get() = object : Bar {
        override val fooInstance: Foo = injectedFoo
    }
}

RuntimeException: toothpick.locators.NoFactoryFoundException: No factory could be found for class com.example.Foo. Check that the class has either a @Inject annotated constructor or contains @Inject annotated members. Caused by: java.lang.ClassNotFoundException: com.example.Foo__Factory