Repository navigation
Replies: 1 comment
|
The Inject the controller factory instead, then call it with those values. Use the factory's documented from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
class Container(containers.DeclarativeContainer):
controller = providers.Factory(
Controller,
some_cls=providers.Factory(SomeCls),
)
class SomeEntity:
@inject
def __init__(
self, arg1, arg2,
controller_factory=Provide[Container.controller.provider],
):
self.arg = arg1
self.arg2 = arg2
self.controller = controller_factory(
arg1=arg1,
arg2=arg2,
some_cls__arg1=arg1,
some_cls__arg2=arg2,
)
# For this single-module example, after the classes are defined:
container = Container()
container.wire(modules=[__name__])
entity = SomeEntity("first", "second")No placeholders are needed in the container declaration. In your multi-module application, wire the module where On the decorator question: References: forwarding factory arguments, injecting a provider through wiring, and wiring implementation. |
Uh oh!
There was an error while loading. Please reload this page.
Hello.
I'm trying to create a Controller object that I expect to be taken from the container to the initializer of the SomeEntity class and so that it is built automatically from the arguments that are next to it (arg1, arg2) in the initializer of the SomeEntity class. I would also like to pass these arguments to the next object (SomeCls) inside the Controller - can I do such a chain?
What should I substitute for the '???' signs? Tried dynamic container but didn't work
Why don’t I see the difference between the @Inject decorator attached to the initializer of the SomeEntity class and without this decorator? All classes are in different modules
All reactions