2016-10-19 20:20:36 +03:00
|
|
|
"""Dependency injection example, Cars & Engines IoC containers."""
|
|
|
|
|
|
|
|
import example.cars
|
|
|
|
import example.engines
|
|
|
|
|
|
|
|
import dependency_injector.containers as containers
|
|
|
|
import dependency_injector.providers as providers
|
|
|
|
|
|
|
|
|
|
|
|
class Engines(containers.DeclarativeContainer):
|
|
|
|
"""IoC container of engine providers."""
|
|
|
|
|
|
|
|
gasoline = providers.Factory(example.engines.GasolineEngine)
|
|
|
|
|
|
|
|
diesel = providers.Factory(example.engines.DieselEngine)
|
|
|
|
|
2020-01-27 02:41:36 +03:00
|
|
|
electric = providers.Factory(example.engines.ElectricEngine)
|
2016-10-19 20:20:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Cars(containers.DeclarativeContainer):
|
|
|
|
"""IoC container of car providers."""
|
|
|
|
|
|
|
|
gasoline = providers.Factory(example.cars.Car,
|
|
|
|
engine=Engines.gasoline)
|
|
|
|
|
|
|
|
diesel = providers.Factory(example.cars.Car,
|
|
|
|
engine=Engines.diesel)
|
|
|
|
|
2020-01-27 02:41:36 +03:00
|
|
|
electric = providers.Factory(example.cars.Car,
|
|
|
|
engine=Engines.electric)
|
2016-10-19 20:20:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
gasoline_car = Cars.gasoline()
|
|
|
|
diesel_car = Cars.diesel()
|
2020-01-27 02:41:36 +03:00
|
|
|
electric_car = Cars.electric()
|