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

support declare a component definition with primary type #773

Merged
merged 2 commits into from
May 29, 2020

Conversation

xudshen
Copy link

@xudshen xudshen commented Apr 10, 2020

Problem

When we want to declare an instance on the fly, the current logic uses the instance's class type as primary type for the bean definition.
But there are sometimes that we might expect using instance's supertype as primary type.

open class Lifecycle
class UserLifecycle1 : Lifecycle()
class UserLifecycle2 : Lifecycle()

val koin = koinApplication {
    printLogger()
    modules(module {
        scope(named("UserSession1")) {
            scoped(named("lifecycle")) { UserLifecycle1() as Lifecycle }
        }
        scope(named("UserSession2")) {
        }
    })
}.koin

val session1 = koin.createScope("session1", named("UserSession1"))
val session2 = koin.createScope("session2", named("UserSession2"))
session2.declare(UserLifecycle2() as Lifecycle, qualifier = named("lifecycle"))

session1.get<Lifecycle>(named("lifecycle")) // return UserLifecycle1 instance
session2.get<UserLifecycle2>(named("lifecycle")) // return UserLifecycle2 instance
session2.get<Lifecycle>(named("lifecycle")) // throw definition not found

scoped(named("lifecycle")) { UserLifecycle1() as Lifecycle } and session2.declare(UserLifecycle2() as Lifecycle, qualifier = named("lifecycle")) should achieve the same effect intuitively.

Solution

I've changed the Scope#declare function and added a clazz parameter to specify the primary type for current definition. Inside ScopeDefinition#saveNewDefinition, it will check if clazz is same as or supertype of provided instance.

// session2.declare(UserLifecycle2() as Lifecycle, qualifier = named("lifecycle"))
session2.declare(UserLifecycle2(), Lifecycle::class, qualifier = named("lifecycle"))

session2.get<Lifecycle>(named("lifecycle")) // return UserLifecycle2 instance as expected

@arnaudgiuliani arnaudgiuliani added type:enhancement status:checking currently in analysis - discussion or need more detailed specs labels May 15, 2020
Copy link
Member

@arnaudgiuliani arnaudgiuliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is the right approach. Can you try to use reified types at Scope API level

@xudshen
Copy link
Author

xudshen commented May 16, 2020

hello, I've changed the approach with reified type parameters. The only downside is making the Definitions#createSingle public.

@arnaudgiuliani arnaudgiuliani added this to the 2.1.6 milestone May 29, 2020
@arnaudgiuliani arnaudgiuliani added status:accepted accepted to be developed and removed status:checking currently in analysis - discussion or need more detailed specs labels May 29, 2020
@arnaudgiuliani arnaudgiuliani merged commit 51f9cd8 into InsertKoinIO:2.1.x May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:accepted accepted to be developed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants