diff --git a/README.rst b/README.rst index 58ae5ed0..426dca06 100644 --- a/README.rst +++ b/README.rst @@ -74,31 +74,39 @@ Key features of the ``Dependency Injector``: .. code-block:: python - from dependency_injector import containers, providers + from dependency_injector import containers, providers + from dependency_injector.wiring import Provide - class Container(containers.DeclarativeContainer): + class Container(containers.DeclarativeContainer): - config = providers.Configuration() + config = providers.Configuration() - api_client = providers.Singleton( - ApiClient, - api_key=config.api_key, - timeout=config.timeout.as_int(), - ) + api_client = providers.Singleton( + ApiClient, + api_key=config.api_key, + timeout=config.timeout.as_int(), + ) - service = providers.Factory( - Service, - api_client=api_client, - ) + service = providers.Factory( + Service, + api_client=api_client, + ) - if __name__ == '__main__': - container = Container() - container.config.api_key.from_env('API_KEY') - container.config.timeout.from_env('TIMEOUT') + def main(service: Service = Provide[Container.service]): + ... - service = container.service() + + if __name__ == '__main__': + container = Container() + + container.config.api_key.from_env('API_KEY') + container.config.timeout.from_env('TIMEOUT') + + container.wire(modules=[sys.modules[__name__]]) + + main() With the ``Dependency Injector`` you keep **application structure in one place**. This place is called **the container**. You use the container to manage all the components of the diff --git a/docs/index.rst b/docs/index.rst index 2c72e9bc..f6b474a3 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -83,31 +83,39 @@ Key features of the ``Dependency Injector``: .. code-block:: python - from dependency_injector import containers, providers + from dependency_injector import containers, providers + from dependency_injector.wiring import Provide - class Container(containers.DeclarativeContainer): + class Container(containers.DeclarativeContainer): - config = providers.Configuration() + config = providers.Configuration() - api_client = providers.Singleton( - ApiClient, - api_key=config.api_key, - timeout=config.timeout.as_int(), - ) + api_client = providers.Singleton( + ApiClient, + api_key=config.api_key, + timeout=config.timeout.as_int(), + ) - service = providers.Factory( - Service, - api_client=api_client, - ) + service = providers.Factory( + Service, + api_client=api_client, + ) - if __name__ == '__main__': - container = Container() - container.config.api_key.from_env('API_KEY') - container.config.timeout.from_env('TIMEOUT') + def main(service: Service = Provide[Container.service]): + ... - service = container.service() + + if __name__ == '__main__': + container = Container() + + container.config.api_key.from_env('API_KEY') + container.config.timeout.from_env('TIMEOUT') + + container.wire(modules=[sys.modules[__name__]]) + + main() With the ``Dependency Injector`` you keep **application structure in one place**. This place is called **the container**. You use the container to manage all the components of the diff --git a/examples/di_demo/demo.py b/examples/demo/demo.py similarity index 66% rename from examples/di_demo/demo.py rename to examples/demo/demo.py index c62c1804..1edb469c 100644 --- a/examples/di_demo/demo.py +++ b/examples/demo/demo.py @@ -1,17 +1,9 @@ +import sys + from dependency_injector import containers, providers +from dependency_injector.wiring import Provide - -class ApiClient: - - def __init__(self, api_key: str, timeout: int): - self.api_key = api_key - self.timeout = timeout - - -class Service: - - def __init__(self, api_client: ApiClient): - self.api_client = api_client +from di import ApiClient, Service class Container(containers.DeclarativeContainer): @@ -30,9 +22,16 @@ class Container(containers.DeclarativeContainer): ) +def main(service: Service = Provide[Container.service]): + ... + + if __name__ == '__main__': container = Container() + container.config.api_key.from_env('API_KEY') container.config.timeout.from_env('TIMEOUT') - service = container.service() + container.wire(modules=[sys.modules[__name__]]) + + main() diff --git a/examples/di_demo/example_di.py b/examples/demo/di.py similarity index 58% rename from examples/di_demo/example_di.py rename to examples/demo/di.py index db85c237..4b34b6a1 100644 --- a/examples/di_demo/example_di.py +++ b/examples/demo/di.py @@ -14,5 +14,15 @@ class Service: self.api_client = api_client +def main() -> None: + service = Service( + api_client=ApiClient( + api_key=os.getenv('API_KEY'), + timeout=os.getenv('TIMEOUT'), + ), + ) + ... + + if __name__ == '__main__': - service = Service(ApiClient(os.getenv('API_KEY'), os.getenv('TIMEOUT'))) + main() diff --git a/examples/di_demo/example_no_di.py b/examples/demo/example.py similarity index 87% rename from examples/di_demo/example_no_di.py rename to examples/demo/example.py index c567ebd6..393b04b3 100644 --- a/examples/di_demo/example_no_di.py +++ b/examples/demo/example.py @@ -14,5 +14,10 @@ class Service: self.api_client = ApiClient() -if __name__ == '__main__': +def main() -> None: service = Service() + ... + + +if __name__ == '__main__': + main() diff --git a/examples/demo/test.py b/examples/demo/test.py new file mode 100644 index 00000000..ce5e0b6c --- /dev/null +++ b/examples/demo/test.py @@ -0,0 +1,11 @@ +from unittest import mock + +import demo + + +if __name__ == '__main__': + container = demo.Container() + container.wire(modules=[demo]) + + with container.api_client.override(mock.Mock()): + demo.main() diff --git a/examples/di_demo/test.py b/examples/di_demo/test.py deleted file mode 100644 index 56310989..00000000 --- a/examples/di_demo/test.py +++ /dev/null @@ -1,11 +0,0 @@ -from unittest import mock - -from demo import Container - - -if __name__ == '__main__': - container = Container() - - with container.api_client.override(mock.Mock()): - service = container.service() - assert isinstance(service.api_client, mock.Mock)