mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 16:07:51 +03:00 
			
		
		
		
	* Add container injections to wiring * Add example * Update docs * Update changelog * Improve typing
		
			
				
	
	
		
			29 lines
		
	
	
		
			509 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			29 lines
		
	
	
		
			509 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| """Wiring container injection example."""
 | |
| 
 | |
| import sys
 | |
| 
 | |
| from dependency_injector import containers, providers
 | |
| from dependency_injector.wiring import inject, Provide
 | |
| 
 | |
| 
 | |
| class Service:
 | |
|     ...
 | |
| 
 | |
| 
 | |
| class Container(containers.DeclarativeContainer):
 | |
| 
 | |
|     service = providers.Factory(Service)
 | |
| 
 | |
| 
 | |
| @inject
 | |
| def main(container: Container = Provide[Container]):
 | |
|     service = container.service()
 | |
|     ...
 | |
| 
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     container = Container()
 | |
|     container.wire(modules=[sys.modules[__name__]])
 | |
| 
 | |
|     main()
 |