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

Permit overwrite and reset from child capsule #4

Merged
merged 8 commits into from
May 3, 2023
Merged

Conversation

mantono
Copy link
Member

@mantono mantono commented Apr 28, 2023

Adding a new dependency of an already existing type should reset the initialization of all dependencies that depend on that class/interface/type so they can be re-resolved with the new dependency for that type.

This test is a good example of something that is now possible that wasn't before:

interface Greeter {
    fun hello(): String
}

class GreeterService(private val greeter: Greeter) {
    fun greet(): String = greeter.hello()
}

class FirstImpl : Greeter {
    override fun hello(): String = "first"
}

class SecondImpl : Greeter {
    override fun hello(): String = "second"
}

@Test
fun `adding new dependency of same interface should invoke a reset`() {
    val parent = Capsule {
        register<Greeter> { FirstImpl() }
        register { GreeterService(get()) }
    }

    assertEquals("first", parent.get<GreeterService>().greet())

    val child = Capsule(parent) {
        register<Greeter> { SecondImpl() }
    }

    assertEquals("second", child.get<GreeterService>().greet())
}

Previously, the last assertion in the test would fail, since the GreeterService would already been initialized once with the first Greeter implementation FirstImpl. Registering a second implementation of the same type in a child capsule would not have had any affect.

This made it very cumbersome when you had tests that would utilize all but a few of the real dependencies that you would normally use at runtime. By changing so whenever a new (child) capsule was greeted, all inherited dependencies that had already been initialized would be reset, and they would also consider registered dependencies in the child module when instantiating this type.

With this change it would be very easy to change resolved dependencies of classes in retrospective, which means less boiler plate when creating tests where most dependencies are not simple mocks or stubs.

Adding a new dependency of an already existing type should reset all
dependencies that depend on that class/interface/type so they can be
re-resolved with the new dependency.
@mantono mantono self-assigned this Apr 28, 2023
@mantono mantono changed the title wip: Improve resolution logic Permit overwrite and reset from child capsule May 3, 2023
@mantono mantono marked this pull request as ready for review May 3, 2023 14:13
@mantono mantono merged commit 7da9f09 into master May 3, 2023
@mantono mantono deleted the fix-reset-on-add branch May 3, 2023 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant