logo

azure-pipeline codecov docs gitter pypi conda-forge black


Dependency Injection for Humans

Dependency Injection (or simply DI) is a great technique. By using it you can organize responsibilities in you codebase. Define high level policies and system behavior in one part. Delegate control to low level mechanisms in anotherpart. Simple and powerful.

With help of DI you can use different parts of your system independently and combine their behavior really easy.

If you split logic and implementation into different classes, you will see how pleasant it becomes to change your system.

This tiny library helps you to connect parts of your system, in particular - to inject low level implementation into high level behavior.

Example

Dependency injection without dependencies

>>> from examples import Robot, Servo, Amplifier, Controller, Settings

>>> robot = Robot(
...     servo=Servo(amplifier=Amplifier()),
...     controller=Controller(),
...     settings=Settings(environment="production"),
... )

>>> robot.work()

Dependency injection with dependencies

>>> from dependencies import Injector

>>> class Container(Injector):
...     robot = Robot
...     servo = Servo
...     amplifier = Amplifier
...     controller = Controller
...     settings = Settings
...     environment = "production"

>>> Container.robot.work()

Installation

Release version

Dependencies is available on PyPI - to install it, just run:

pip install -U dependencies

That's it! Once installed, dependencies library is available for use. Import it and have fun.

Development version

You can always install last development version directly from source control:

pip install -U git+https://github.com/dry-python/dependencies.git

— ⭐️ —

Drylabs maintains dry-python and helps those who want to use it inside their organizations.

Read more at drylabs.io