From cfdcbaa77ae2c53ab4db32b271b2b5b776e992de Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Sat, 15 Aug 2020 22:09:41 -0400 Subject: [PATCH] Improve declarative and dynamic container docs --- docs/containers/declarative.rst | 19 +++-- docs/containers/dynamic.rst | 27 +++---- examples/containers/declarative.py | 17 +++-- examples/containers/declarative_injections.py | 11 +-- .../declarative_override_providers.py | 7 +- examples/containers/dynamic.py | 26 +++---- .../containers/dynamic_runtime_creation.py | 73 +++++++------------ 7 files changed, 80 insertions(+), 100 deletions(-) diff --git a/docs/containers/declarative.rst b/docs/containers/declarative.rst index d193b83e..5f362b31 100644 --- a/docs/containers/declarative.rst +++ b/docs/containers/declarative.rst @@ -1,23 +1,22 @@ -Declarative containers ----------------------- +Declarative container +--------------------- .. currentmodule:: dependency_injector.containers -:py:class:`DeclarativeContainer` is a collection of the providers defined in the declarative -manner. It covers the use cases when your application structure does not change in the runtime. +:py:class:`DeclarativeContainer` is a class-based style of the providers definition. -Container has the ``.providers`` attribute. It is a dictionary of the container providers. +You create the declarative container subclass, put the providers as attributes and create the +container instance. .. literalinclude:: ../../examples/containers/declarative.py :language: python :lines: 3- -Your declarative container has to extend base declarative container class - -:py:class:`dependency_injector.containers.DeclarativeContainer`. +The declarative container providers should only be used when you have the container instance. +Working with the providers of the container on the class level will influence all further +instances. -Declarative container classes can not have any methods or any other attributes then providers. - -The declarative container providers should only be used after the container is initialized. +The declarative container can not have any methods or any other attributes then providers. The container class provides next attributes: diff --git a/docs/containers/dynamic.rst b/docs/containers/dynamic.rst index bd879f23..856ad7c4 100644 --- a/docs/containers/dynamic.rst +++ b/docs/containers/dynamic.rst @@ -1,28 +1,25 @@ -Dynamic containers ------------------- +Dynamic container +----------------- .. currentmodule:: dependency_injector.containers -:py:class:`DynamicContainer` is an inversion of control container with dynamic -structure. It should cover most of the cases when list of providers that -would be included in container is non-deterministic and depends on -application's flow or its configuration (container's structure could be -determined just after application will be started and will do some initial -work, like parsing list of container's providers from the configuration). +:py:class:`DynamicContainer` is a collection of the providers defined in the runtime. -While :py:class:`DeclarativeContainer` acts on class-level, -:py:class:`DynamicContainer` does the same on instance-level. - -Here is an simple example of defining dynamic container with several factories: +You create the dynamic container instance and put the providers as attributes. .. literalinclude:: ../../examples/containers/dynamic.py :language: python + :lines: 3- -Next example demonstrates creation of dynamic container based on some -configuration: +The dynamic container is good for the case when your application structure depends on the +configuration file or some other source that you can reach only after application is already +running (database, api, etc). + +In this example we use the configuration to fill in the dynamic container with the providers: .. literalinclude:: ../../examples/containers/dynamic_runtime_creation.py :language: python - + :lines: 3- .. disqus:: + diff --git a/examples/containers/declarative.py b/examples/containers/declarative.py index 1654e8fe..4fb7d795 100644 --- a/examples/containers/declarative.py +++ b/examples/containers/declarative.py @@ -10,13 +10,14 @@ class Container(containers.DeclarativeContainer): factory2 = providers.Factory(object) -container = Container() +if __name__ == '__main__': + container = Container() -object1 = container.factory1() -object2 = container.factory2() + object1 = container.factory1() + object2 = container.factory2() -print(container.providers) -# { -# 'factory1':