Closed terekhovyes closed 3 years ago
About 1'st. Check extension Scope.getInstance()
About 1'st. Check extension
Scope.getInstance()
No, you have no access to Scope
inside toProviderInstance {}
function, so it won't work
About 1'st. Check extension
Scope.getInstance()
No, you have no access to
Scope
insidetoProviderInstance {}
function, so it won't work
It is 2'nd question :)
Not sure if I fully understand the problem correctly looking at the code. With Koin/Kodein you need to define providers for every single class that you want to be able to inject. With TP there is no need for that. Using your example, this would work:
@InjectConstructor
class Printer(val helloWorld: HelloWorld)
fun helloModule(name: String) {
bind<HelloWorld>().toProviderInstance { HelloWorld(name) }.providesSingleton()
bind<Printer>().singleton()
}
TP when trying to inject HelloWorld
inside Printer
would use the one defined inside helloModule
.
Still if you have a different use case where you still need to inject something inside the provider, i see 2 options:
val scope = KTP.openScope(name)
scope.installModules(module {
bind<HelloWorld>().toProviderInstance { HelloWorld(name) }.providesSingleton()
bind<Printer>().toProviderInstance { Printer(scope.getInstance()) } .providesSingleton()
})
.inject(this)
Btw, sorry for the delay...
everything is working
class HelloWorld @Inject constructor(val name: String)
class Printer @Inject constructor(val helloWorld: HelloWorld)
rootScope.installModules(module {
bind(HelloWorld::class.java).toProviderInstance { HelloWorld("name") }.providesSingleton()
bind(Printer::class.java).singleton()
})
In other libs (Koin, Kodein) I love simplicity of "Provider" declaration. But I don't see any ways to do it with Toothpick. So instead of writing something like this (look at
get()
function):I have to write boilerplate Provider classes:
So the questions are:
get()
function, that inject dependencies from current scope?Scope
right insidemodule {}
function?