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

recursive_wire #817

Open
lagunovartur opened this issue Sep 5, 2024 · 1 comment
Open

recursive_wire #817

lagunovartur opened this issue Sep 5, 2024 · 1 comment

Comments

@lagunovartur
Copy link

lagunovartur commented Sep 5, 2024

Приветствую, хотел поинтересоваться почему в библиотеке нет рекурсивного связывания на подобие такого, это позволяет делать инъекции не от корня, а от ближайшего родителя:

Например:
Так:
@router.get("/test")
@Inject
async def get_list(s3_manager = Depends(Provide[S3Container.manager])):
pass

Вместо:
@router.get("/test")
@Inject
async def get_list(s3_client = Depends(Provide[AppContainer.infra.s3.manager])):
pass

Рекурсивное связывание:

def recursive_wire(container, *args, **kwargs):
   
    container.wire(*args, **kwargs)

    for attr_name in dir(container):
        if attr_name.startswith('_') or attr_name == 'parent':
            continue
        attr = getattr(container, attr_name, None)
        if isinstance(attr, providers.Container):
            sub_container = attr() 
            recursive_wire(sub_container, *args, **kwargs)
@LeOndaz
Copy link

LeOndaz commented Oct 12, 2024

Translation

Hello,

I would like to inquire about the absence of recursive binding in the library, similar to the following. This feature allows for dependency injection from the nearest parent, rather than from the root.

For example:

@router.get("/test")
@Inject
async def get_list(s3_manager = Depends(Provide[S3Container.manager])):
    pass

Instead of:

@router.get("/test")
@Inject
async def get_list(s3_client = Depends(Provide[AppContainer.infra.s3.manager])):
    pass

Recursive binding:

def recursive_wire(container, *args, **kwargs):
   
    container.wire(*args, **kwargs)

    for attr_name in dir(container):
        if attr_name.startswith('_') or attr_name == 'parent':
            continue
        attr = getattr(container, attr_name, None)
        if isinstance(attr, providers.Container):
            sub_container = attr() 
            recursive_wire(sub_container, *args, **kwargs)

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