ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
3.82k stars 303 forks source link

DynamicContainer how/why behaves differently .. Flask Blueprints #471

Open xpegenaute opened 3 years ago

xpegenaute commented 3 years ago

Hi!,

I am considering the use of your library and I want to be able to define Container's providers in runtime. For this I want to use a DynamicContair. AFAIK this should fit prefectly.

Once I started to test it I used your flask-blueprints example [1]. After changing the line which refers to the Container definition [2] to inherit from DynamicContainer everything fails. How should I modify the example to work properly with DynamicContainers ?, in the doc [3] there is not any special mention to anything.

Regards, Xavi

[1] - https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/flask-blueprints [2] - https://github.com/ets-labs/python-dependency-injector/blob/54de3a9d2c2920b3b96222f1b0b475513a3025fc/examples/miniapps/flask-blueprints/githubnavigator/containers.py#L9 [3] - https://python-dependency-injector.ets-labs.org/containers/dynamic.html

rmk135 commented 3 years ago

Hi @xpegenaute ,

With dynamic container you don't need to inherit it with a subclass, just create an instance:

"""Containers module."""

from dependency_injector import containers, providers
from github import Github

from . import services

container = containers.DynamicContainer()

container.config = providers.Configuration()

container.github_client = providers.Factory(
    Github,
    login_or_token=container.config.github.auth_token,
    timeout=container.config.github.request_timeout,
)

container.search_service = providers.Factory(
    services.SearchService,
    github_client=container.github_client,
)

PS: I didn't test it with an app, but hope that helps. If that doesn't work, let me know