Wiring

Wiring feature provides a way to inject container providers into the functions and methods.

To use wiring you need:

  • Place @inject decorator. Decorator @inject injects the dependencies.

  • Place markers. Wiring marker specifies what dependency to inject, e.g. Provide[Container.bar]. This helps container to find the injections.

  • Wire the container with the markers in the code. Call container.wire() specifying modules and packages you would like to wire it with.

  • Use functions and classes as you normally do. Framework will provide specified injections.

import sys

from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide


class Service:
    ...


class Container(containers.DeclarativeContainer):

    service = providers.Factory(Service)


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


if __name__ == '__main__':
    container = Container()
    container.wire(modules=[sys.modules[__name__]])

    main()

Markers

Wiring feature uses markers to make injections. Injection marker is specified as a default value of a function or method argument:

from dependency_injector.wiring import inject, Provide


@inject
def foo(bar: Bar = Provide[Container.bar]):
    ...

Specifying an annotation is optional.

There are two types of markers:

  • Provide[foo] - call the provider foo and injects the result

  • Provider[foo] - injects the provider foo itself

from dependency_injector.wiring import inject, Provider


@inject
def foo(bar_provider: Callable[..., Bar] = Provider[Container.bar]):
    bar = bar_provider()
    ...

You can use configuration, provided instance and sub-container providers as you normally do.

@inject
def foo(token: str = Provide[Container.config.api_token]):
    ...


@inject
def foo(timeout: int = Provide[Container.config.timeout.as_(int)]):
    ...


@inject
def foo(baz: Baz = Provide[Container.bar.provided.baz]):
    ...


@inject
def foo(bar: Bar = Provide[Container.subcontainer.bar]):
    ...

You can compound wiring and Resource provider to implement per-function execution scope. See Resources, wiring and per-function execution scope for details.

Wiring with modules and packages

To wire a container with a module you need to call container.wire(modules=[...]) method. Argument modules is an iterable of the module objects.

from yourapp import module1, module2


container = Container()
container.wire(modules=[module1, module2])

You can wire container with a package. Container walks recursively over package modules.

from yourapp import package1, package2


container = Container()
container.wire(packages=[package1, package2])

Arguments modules and packages can be used together.

When wiring is done functions and methods with the markers are patched to provide injections when called.

@inject
def foo(bar: Bar = Provide[Container.bar]):
    ...


container = Container()
container.wire(modules=[sys.modules[__name__]])

foo()  # <--- Argument "bar" is injected

Injections are done as keyword arguments.

foo()  # Equivalent to:
foo(bar=container.bar())

Context keyword arguments have a priority over injections.

foo(bar=Bar())  # Bar() is injected

To unpatch previously patched functions and methods call container.unwire() method.

container.unwire()

You can use that in testing to re-create and re-wire a container before each test.

import unittest


class SomeTest(unittest.TestCase):

    def setUp(self):
        self.container = Container()
        self.container.wire(modules=[module1, module2])
        self.addCleanup(self.container.unwire)
import pytest


@pytest.fixture
def container():
    container = Container()
    container.wire(modules=[module1, module2])
    yield container
    container.unwire()

Note

Wiring can take time if you have a large codebase. Consider to persist a container instance and avoid re-wiring between tests.

Note

Python has a limitation on patching already imported individual members. To protect from errors prefer an import of modules instead of individual members or make sure that imports happen after the wiring:

from . import module

module.fn()

# instead of

from .module import fn

fn()

Integration with other frameworks

Wiring feature helps to integrate with other frameworks like Django, Flask, etc.

With wiring you do not need to change the traditional application structure of your framework.

  1. Create a container and put framework-independent components as providers.

  2. Place wiring markers in the functions and methods where you want the providers to be injected (Flask or Django views, Aiohttp or Sanic handlers, etc).

  3. Wire the container with the application modules.

  4. Run the application.

import sys

from dependency_injector import containers, providers
from dependency_injector.wiring import inject, Provide
from flask import Flask, json


class Service:
    ...


class Container(containers.DeclarativeContainer):

    service = providers.Factory(Service)


@inject
def index_view(service: Service = Provide[Container.service]) -> str:
    return json.dumps({'service_id': id(service)})


if __name__ == '__main__':
    container = Container()
    container.wire(modules=[sys.modules[__name__]])

    app = Flask(__name__)
    app.add_url_rule('/', 'index', index_view)
    app.run()

Take a look at other application examples: