python-dependency-injector/examples/speech/container.py

27 lines
690 B
Python
Raw Normal View History

2016-06-02 20:25:45 +03:00
"""IoC container example."""
import collections
import dependency_injector.containers as containers
import dependency_injector.providers as providers
2016-06-03 00:47:10 +03:00
Engine = collections.namedtuple('Engine', [])
Car = collections.namedtuple('Car', ['serial_number', 'engine'])
2016-06-02 20:25:45 +03:00
class Container(containers.DeclarativeContainer):
"""IoC container."""
2016-06-03 00:47:10 +03:00
engine_factory = providers.Factory(Engine)
car_factory = providers.Factory(Car, engine=engine_factory)
2016-06-02 20:25:45 +03:00
if __name__ == '__main__':
2016-06-03 00:47:10 +03:00
car1 = Container.car_factory(serial_number=1)
car2 = Container.car_factory(serial_number=2)
2016-06-02 20:25:45 +03:00
2016-06-03 00:47:10 +03:00
assert car1.serial_number == 1 and car2.serial_number == 2
2016-06-02 20:25:45 +03:00
assert car1.engine is not car2.engine