This example shows how to use Dependency Injector
to create decoupled packages.
To achieve a decoupling each package has a container with the components. When a component needs a
dependency from the outside of the package scope we use the Dependency
provider. The package
container has no knowledge on where the dependencies come from. It states a need that the
dependencies must be provided. This helps to decouple a package from the 3rd party dependencies
and other packages.
To wire the packages we use an application container. Application container has all 3rd party dependencies and package containers. It wires the packages and dependencies to create a complete application.
We build an example micro application that consists of 3 packages:
user
- a package with user domain logic, depends on a databasephoto
- a package with photo domain logic, depends on a database and AWS S3analytics
- a package with analytics domain logic, depends on theuser
andphoto
package components
Start from the scratch or jump to the section:
You can find the source code and instructions for running on the Github.
Application consists of an example
package, a configuration file and a requirements.txt
file.
./
├── example/
│ ├── abstraction
│ │ ├── __init__.py
│ │ ├── analytics
│ │ │ ├── __init__.py
│ │ │ └── services.py
│ │ ├── photo
│ │ │ ├── __init__.py
│ │ │ └── repositories.py
│ │ └── user
│ │ ├── __init__.py
│ │ └── repositories.py
│ ├── analytics/
│ │ ├── __init__.py
│ │ ├── containers.py
│ │ └── services.py
│ ├── photo/
│ │ ├── __init__.py
│ │ ├── containers.py
│ │ ├── entities.py
│ │ └── repositories.py
│ ├── user/
│ │ ├── __init__.py
│ │ ├── containers.py
│ │ ├── entities.py
│ │ └── repositories.py
│ ├── __init__.py
│ ├── __main__.py
│ └── containers.py
├── config.ini
└── requirements.txt
Listing of the example/abstraction/user/repositories.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/abstraction/user/repositories.py :language: python
Listing of the example/abstraction/photo/repositories.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/abstraction/photo/repositories.py :language: python
Listing of the example/abstraction/analytics/services.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/abstraction/analytics/services.py :language: python
Listing of the example/user/containers.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/user/containers.py :language: python
Listing of the example/photo/containers.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/photo/containers.py :language: python
Listing of the example/analytics/containers.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/analytics/containers.py :language: python
Application container consists of all packages and 3rd party dependencies. Its role is to wire everything together in a complete application.
Listing of the example/containers.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/containers.py :language: python
Note
Package analytics
has dependencies on the repositories from the user
and
photo
packages. This is an example of how you can pass the dependencies from one package
to another.
Listing of the example/__main__.py
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/example/__main__.py :language: python
Listing of the config.ini
:
.. literalinclude:: ../../examples/miniapps/decoupled-packages/config.ini :language: ini
You can find the source code and instructions for running on the Github.
.. disqus::