I haven’t looked much at Scopes, so I’ll have to c...
# kodein
j
I haven’t looked much at Scopes, so I’ll have to check that out. Thanks!
s
You’re welcome. 🙂 Create an
object
that implements your custom Scope and use that for your dependency definitions. Then access that
object
to ‘clear it out’ when needed.
(not sure if messing with a ‘mutable’ Scope is the official or desired way to do this, but it works for me)
j
Can you clarify “access the `object`“? Do you mean delete it?
s
In your dependency definitions, use the
myScope
as the Scope for the dependencies you’d like to be able to throw away.
Copy code
object myScope: Scope<SomeContext> { 
    override fun getRegistry(context: SomeContext): ScopeRegistry {
        ... 
    }

    fun clear() {
        ...
    }
}
And when you want to ‘throw it away’ call
clear
.
Copy code
myScope.clear()
j
Ah okay, now I see. That looks like exactly what I want. Thanks!
s
Note that you can also use a
ConfigurableKodein
and re-create your dependencies (that is what I use for running unit-tests). Also, consider using plain `Scope`s or tags if you need to get a different dependency (singleton) given a different context of your app. The hack of ‘clearing’ your Scope is just that, a hack… 🙂