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

Access original instance from within overriding statement #43

Closed
dimorinny opened this issue Dec 2, 2016 · 3 comments
Closed

Access original instance from within overriding statement #43

dimorinny opened this issue Dec 2, 2016 · 3 comments

Comments

@dimorinny
Copy link

dimorinny commented Dec 2, 2016

In tests I want to wrap real object with some test logic and pass wrapped instance to application.

Now for this case, I also provide implementation of object and use it only in override-provider, so it looks like this:

// Module in real Application
...
bind<AvsDoActionExecutor>() with singleton {
    instance<AvsDoActionExecutorImpl>()
}

bind<AvsDoActionExecutorImpl>() with singleton {
    AvsDoActionExecutorImpl(
        // something
    )
}
// Module with override flag in test Application
...
bind<AvsDoActionExecutor>() with singleton {
    TestLogicWrapper(instance<AvsDoActionExecutorImpl>())
}

Are there ways to do it natively in Kodein 2.8.0? Is there any way to access original instance from within overriding statement?

@SalomonBrys
Copy link
Member

No, there's no better way, sorry.

Though, for this use case, I wouldn't bind directly AvsDoActionExecutorImpl but create a second bind of AvsDoActionExecutor with a tag.

// Module in real Application
...
bind<AvsDoActionExecutor>() with singleton {
    instance<AvsDoActionExecutor>("actual")
}

bind<AvsDoActionExecutor>("actual") with singleton {
    AvsDoActionExecutorImpl(
        // something
    )
}
// Module with override flag in test Application
...
bind<AvsDoActionExecutor>() with singleton {
    TestLogicWrapper(instance<AvsDoActionExecutor>("actual"))
}

I realize this is not ideal. However, the branch 2.x is not developped anymore (unless for bug fixes).

In branch 3.x, allowing overrides to access the overriden binding is an interesting idea. I'm not sure how much it would complicate the code. I'll think on it ;)

@SalomonBrys
Copy link
Member

I'm happy to report that access to overrides will be part of Kodein 3.2.0.

Your use case will be as simple as:

// Module in real Application
...
bind<AvsDoActionExecutor>() with singleton {
    AvsDoActionExecutorImpl(
        // something
    )
}
// Module with override flag in test Application
...
bind<AvsDoActionExecutor>() with singleton {
    TestLogicWrapper(overriddenInstance<AvsDoActionExecutor>())
}

Thanks for the great feature idea ;)

Salomon.

@dimorinny
Copy link
Author

It's great. Thanks 👍

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

No branches or pull requests

2 participants