Skip to content

Question: Possibility of defining dependency parameters for a container on call site #656

Open
@wax911

Description

@wax911
class Service:
    ...
from dependency_injector import containers, providers


class Container(containers.DeclarativeContainer):
    # obtain `environment` as a passed in parameter
    environment = providers.Dependency()

    # utilise the provided `environment`
    service = providers.Factory(Service, env=environment.provided)
from dependency_injector.wiring import Provide, inject


@inject
def start(service: Service = Provide['service']) -> None:
    ...


if __name__ == "__main__":
    # example of passing a param into our container
    container = Container(environment='DEBUG')
    container.wire(modules=[__name__])

    start()

Given the above example, with a dependency provided as environment I would like to know if something like this exists within the dependency_injector package, I had a look at the documentation and couldn't really figure out a way to achieve this, and this is as far as I got which doesn't work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions