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

Circular dependency in wiring #764

Open
krunaldodiya opened this issue Nov 12, 2023 · 1 comment
Open

Circular dependency in wiring #764

krunaldodiya opened this issue Nov 12, 2023 · 1 comment

Comments

@krunaldodiya
Copy link

krunaldodiya commented Nov 12, 2023

I am facing the circular dependency issue:

from dependency_injector import containers, providers

from trader.core.args_manager import ArgsManager
from trader.core.api import Api


class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    args_manager = providers.Singleton(
        ArgsManager,
    )

    api = providers.Singleton(
        Api,
    )
from dependency_injector.wiring import Provide, inject

class ArgsManager:
     @inject
     def __init__(self, api = Provide["api"]):
            print(api)
class Api:
     def __init__(self):
            pass

print(api) gives instance of dependency_injector.wiring.Provide instead of Api

if I add wire, it gives circular import error.

ArgsManager requires Container and Container requires ArgsManager

from dependency_injector.wiring import Provide, inject

class ArgsManager:
     @inject
     def __init__(self, api = Provide["api"]):
            print(api)

if __name__ == "__main__":
    container = Container()
    container.wire(modules=[__name__])

How to solve this ?

Originally posted by @krunaldodiya in #327 (comment)

@benm5678
Copy link

benm5678 commented Mar 9, 2024

I'm facing the same issue. I almost get the sense you can only inject objects that don't depend on the container (or any of their dependencies do).... but that makes it so much weaker than other DI frameworks I've used. Does anyone have some solution or another recommendation?

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