You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classPresenter(overridevaldi:DI): DIAware {
privateval db:Database by instance()
privateval rnd:Random by instance()
}
This has worked great for my usecases so far. However, I started using the context system in this sort of fashion:
// As an example, di is initialized earlier with:
bind<Thing> { contexted<MyContext>().provider { context.getThing() } }
classThingUser(overridevaldi:DI) : DIAware {
val theThing by instance<Thing>()
}
val diWithContext = di.on(context =MyContext())
ThingUser(diWithContext)
If ThingUser attempts to access its theThing, you'll get an exception similar to: org.kodein.di.DI$NotFoundException: No binding found for Thing on context ThingUser
This isn't super obvious as to why. However, since DI also extends DIAware, just passing in and overriding the di in ThingUser isn't enough to carry the context through. Instead I found that changing the signature of the class resolves the issue:
classThingUser(di:DI) : DIAware by di
which uses the delegate implementation feature in Kotlin. You can still selectively override DIAware properties in ThingUser if needed.
My question is: is this pattern recommended? Since it also handles the context and trigger, I feel like this seems like the right way to do this rather than just overriding di for DIAware per the guide.
If this seems expected I'd be happy to help update the documentation to add a snippet regarding this.
The text was updated successfully, but these errors were encountered:
This is a question regarding with what I've read in the guide and not a bug with Kodein.
In the Retrieval section (https://kosi-libs.org/kodein/7.22/getting-started.html#_retrieval), there's a simple example of using the DIAware interface:
This has worked great for my usecases so far. However, I started using the context system in this sort of fashion:
If
ThingUser
attempts to access itstheThing
, you'll get an exception similar to:org.kodein.di.DI$NotFoundException: No binding found for Thing on context ThingUser
This isn't super obvious as to why. However, since
DI
also extendsDIAware
, just passing in and overriding thedi
inThingUser
isn't enough to carry the context through. Instead I found that changing the signature of the class resolves the issue:which uses the delegate implementation feature in Kotlin. You can still selectively override DIAware properties in ThingUser if needed.
My question is: is this pattern recommended? Since it also handles the context and trigger, I feel like this seems like the right way to do this rather than just overriding
di
for DIAware per the guide.If this seems expected I'd be happy to help update the documentation to add a snippet regarding this.
The text was updated successfully, but these errors were encountered: