Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dynamic object injector #825

Unanswered
tcpdcp22 asked this question in Q&A
Discussion options

Hello.

  1. 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

  2. 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

from dependency_injector import providers, containers
from dependency_injector.wiring import inject, Provide
class SomeCls:
 def __init__(self, arg1, arg2):
 self.arg = arg1
 self.arg2 = arg2
class Controller:
 def __init__(self, arg1, arg2, some_cls: SomeCls):
 self.a = arg1
 self.b = arg2
 self.cls = some_cls
class Container(containers.DeclarativeContainer):
 controller = providers.Factory(
 Controller,
 arg1=???,
 arg2=???,
 some_cls=providers.Factory(
 SomeCls,
 arg1=???,
 arg2=???,
 )
 )
class SomeEntity:
 @inject
 def __init__(self, arg1, arg2, controller: Controller = Provide[Container.controller]):
 self.arg = arg1
 self.arg2 = arg2
 self.controller = controller
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /