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.
Dependency injection without
>>> 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
>>> from dependencies import Injector >>> class Container(Injector): ... robot = Robot ... servo = Servo ... amplifier = Amplifier ... controller = Controller ... settings = Settings ... environment = "production" >>> Container.robot.work()
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.
You can always install last development version directly from source control:
pip install -U git+https://github.com/dry-python/dependencies.git