Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested Scopes #513

Closed
valeriyo opened this issue Jun 27, 2019 · 6 comments
Closed

Nested Scopes #513

valeriyo opened this issue Jun 27, 2019 · 6 comments
Labels
status:checking currently in analysis - discussion or need more detailed specs

Comments

@valeriyo
Copy link

valeriyo commented Jun 27, 2019

Is your feature request related to a problem? Please describe.
Currently, with Koin 2.0 it's possible to declare nested scopes:

module {
    scope(A) {
        scoped(NUMBER) { 123 }

        // nested scope!
        scope(B) {
            scoped(STRING) { get(NUMBER).toString() }
        }
    }
}

With implied understanding that:

  1. An instance of scope B can be created from an instance of scope A (but not from the global Koin),
  2. Scope B "sees" all bindings from scope A.
  3. Closing an instance of scope A would close all instances of B, which were created from it (i.e. child scopes)

However, that's not what's happening. In Koin 2.0, the above DSL is equivalent to:

module {
    scope(A) {
        scoped(NUMBER) { 123 }
    }

    // actually, it's not nested :(
    scope(B) {
        scoped(STRING) { get(NUMBER).toString() }
    }
}

... and at runtime, the get(NUMBER) will throw a NoBeanDefFoundException.

Koin should do better than that.

Describe the solution you'd like
Nested scoped should be supported. For that, two things are necessary:

  1. ScopeSet needs to define fun scope similar to Module.
  2. Scope needs to define fun createScope similar to Koin - for the nested scopes.

Describe alternatives you've considered
An alternative to nested scopes is doing extra work (like duplicating same bindings across multiple scopes - but that will break singletons)

@valeriyo
Copy link
Author

valeriyo commented Jun 28, 2019

The approach I settled on is:

module {
    scope(A) {
        scoped(NUMBER) { 123 }
    }

    scope(B) {
        scoped(STRING) { scopeA.get<Int>(NUMBER).toString() }
    }
}

where scopeA is an extension property of Scope doing something like GlobalContext.get().koin.getScope("A") - assuming, there is an instance of scope A with name "A".

@arnaudgiuliani arnaudgiuliani added type:feature-proposal status:checking currently in analysis - discussion or need more detailed specs labels Jul 12, 2019
@arnaudgiuliani
Copy link
Member

the most difficult part here is to avoid mixing scope instance id and scope qualifier: The DSL allow you to define a scope with a qualifier. A scope instance id is created/given at runtime.

@arnaudgiuliani arnaudgiuliani added this to Feature proposal or requests in Koin Dev Board Oct 10, 2019
@arnaudgiuliani arnaudgiuliani moved this from New to Analysing in Koin Dev Board Oct 10, 2019
@arnaudgiuliani arnaudgiuliani moved this from Analysing to TODO in Koin Dev Board Dec 17, 2019
@arnaudgiuliani
Copy link
Member

this will be redesign as "Scope Links", to let you establish links between your scope instances.

Koin Dev Board automation moved this from TODO to Done for 2.1.0 Dec 17, 2019
@aisanu
Copy link

aisanu commented Feb 25, 2020

What's the status of this "Scope Links" stuff? Anywhere i could read the docs or at least the implementation code/related PR and the architecture design for this feature?

@kpovilaitis
Copy link

any news?

@nschwermann
Copy link

nschwermann commented Apr 17, 2020

Says complete in 2.1 but still not working. You can do

scope<A>{
    scoped<B>{}
    scope<C>{
           scoped<D>{
                 linkTo(A.scope) //Must be inside scope block, unfortunate if multiple scoped items in scope C depending on B
                 D(get()) //get() B from scope A
            }
     }
}

Works but not ideal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:checking currently in analysis - discussion or need more detailed specs
Projects
None yet
Development

No branches or pull requests

5 participants