Scope API

declare scoped instances in Koin

What’s a scope?

A scope is a fixed duration of time in which an object exists. When the scope context ends, any objects bound under that scope cannot be injected again (they are dropped from the container).

Declare a scoped definition

By default in Koin, we have 3 kind of scopes:

  • single definition, create an object that persistent with the entire container lifetime (can’t be dropped).
  • factory definition, create a new object each time. No persistence in the container (can’t be shared).
  • scoped definition, create an object that persistent tied to the associated scope lifetime.

To declare a scope definition, use the scoped function:

module {
    scoped { Presenter() }
}

Declare and use a scope

A scope is a logical unit of scoped definitions.

For the given classes:

class ComponentA
class ComponentB(val a : class ComponentA)

We can write the following scope:

module {
    // define the scope with its qualifier "MY_SCOPE"
    scope(named("MY_SCOPE") {
        scoped { ComponentA() }
        scoped { ComponentB(get()) }
    }
}

Create and use a scope instance

From any Koin instance or also available from your KoinComponent with getKoin(), the following functions allows to handle scope instances:

// create scope instance with ScopeID "myScope" for scope naled "MY_SCOPE"
val myScope : Scope = koin.createScope("myScope", named("MY_SCOPE")

// get instance ComponentA from scope
val a = myScope.get<ComponentA>()
// get instance ComponentB from scope and retrieve instance ComponentA from same scope
val b = myScope.get<ComponentB>()

// if you need, you can retrieve your scope instance by its id
val myScope : Scope = koin.getScope("myScope")

// close the scope when needed
myScope.close()

More about

Below are some further readings: