mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	Update dependency injection example in introduction/what_is_di.rst
This commit is contained in:
		
							parent
							
								
									63bc3f95e7
								
							
						
					
					
						commit
						a8c9219d09
					
				|  | @ -81,14 +81,14 @@ Example | ||||||
| 
 | 
 | ||||||
| Let's go through the code of ``example.py``: | Let's go through the code of ``example.py``: | ||||||
| 
 | 
 | ||||||
| .. literalinclude:: ../../examples/ioc_di_demos/example.py | .. literalinclude:: ../../examples/di_demo/example.py | ||||||
|    :language: python |    :language: python | ||||||
|    :linenos: |    :linenos: | ||||||
| 
 | 
 | ||||||
| At some point, things defined above mean, that the code from ``example.py``,  | At some point, things defined above mean, that the code from ``example.py``,  | ||||||
| could look different, like in ``example_ioc.py``: | could look different, like in ``example_di.py``: | ||||||
| 
 | 
 | ||||||
| .. literalinclude:: ../../examples/ioc_di_demos/example_ioc.py | .. literalinclude:: ../../examples/di_demo/example_di.py | ||||||
|    :language: python |    :language: python | ||||||
|    :linenos: |    :linenos: | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| """The Code, that follows inversion of control principle.""" | """The Code, that demonstrates dependency injection pattern.""" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Service(object): | class Service(object): | ||||||
|  | @ -1,17 +0,0 @@ | ||||||
| """Car & Engine example.""" |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class Engine(object): |  | ||||||
|     """Example engine.""" |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class Car(object): |  | ||||||
|     """Example car.""" |  | ||||||
| 
 |  | ||||||
|     def __init__(self): |  | ||||||
|         """Initializer.""" |  | ||||||
|         self.engine = Engine()  # Engine is a "hardcoded" dependency |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| if __name__ == '__main__': |  | ||||||
|     car = Car()  # Application creates Car's instance |  | ||||||
|  | @ -1,18 +0,0 @@ | ||||||
| """Refactored Car & Engine example that demonstrates inversion of control.""" |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class Engine(object): |  | ||||||
|     """Example engine.""" |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class Car(object): |  | ||||||
|     """Example car.""" |  | ||||||
| 
 |  | ||||||
|     def __init__(self, engine): |  | ||||||
|         """Initializer.""" |  | ||||||
|         self.engine = engine  # Engine is an "injected" dependency |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| if __name__ == '__main__': |  | ||||||
|     engine = Engine()  # Application creates Engine's instance |  | ||||||
|     car = Car(engine)  # and inject it into the Car's instance |  | ||||||
|  | @ -1,19 +0,0 @@ | ||||||
| """Example of inversion of control container for Car & Engine example.""" |  | ||||||
| 
 |  | ||||||
| import dependency_injector.containers as containers |  | ||||||
| import dependency_injector.providers as providers |  | ||||||
| 
 |  | ||||||
| import car_engine_ioc |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class Container(containers.DeclarativeContainer): |  | ||||||
|     """IoC container of component providers.""" |  | ||||||
| 
 |  | ||||||
|     engine = providers.Factory(car_engine_ioc.Engine) |  | ||||||
| 
 |  | ||||||
|     car = providers.Factory(car_engine_ioc.Car, |  | ||||||
|                             engine=engine) |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| if __name__ == '__main__': |  | ||||||
|     car = Container.car()  # Application creates Car's instance |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user