mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 09:57:37 +03:00 
			
		
		
		
	Update wiring docs and examples
This commit is contained in:
		
							parent
							
								
									357e618dce
								
							
						
					
					
						commit
						315f0c0a62
					
				| 
						 | 
				
			
			@ -216,7 +216,7 @@ execution scope. For doing this you need to use additional ``Closing`` marker fr
 | 
			
		|||
.. literalinclude:: ../../examples/wiring/flask_resource_closing.py
 | 
			
		||||
   :language: python
 | 
			
		||||
   :lines: 3-
 | 
			
		||||
   :emphasize-lines: 23
 | 
			
		||||
   :emphasize-lines: 24
 | 
			
		||||
 | 
			
		||||
Framework initializes and injects the resource into the function. With the ``Closing`` marker
 | 
			
		||||
framework calls resource ``shutdown()`` method when function execution is over.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,8 @@ Wiring feature provides a way to inject container providers into the functions a
 | 
			
		|||
 | 
			
		||||
To use wiring you need:
 | 
			
		||||
 | 
			
		||||
- **Place markers in the code**. Wiring marker specifies what provider to inject,
 | 
			
		||||
- **Place @inject decorator**. Decorator ``@inject`` injects the dependencies.
 | 
			
		||||
- **Place markers**. Wiring marker specifies what dependency to inject,
 | 
			
		||||
  e.g. ``Provide[Container.bar]``. This helps container to find the injections.
 | 
			
		||||
- **Wire the container with the markers in the code**. Call ``container.wire()``
 | 
			
		||||
  specifying modules and packages you would like to wire it with.
 | 
			
		||||
| 
						 | 
				
			
			@ -25,9 +26,10 @@ a function or method argument:
 | 
			
		|||
 | 
			
		||||
.. code-block:: python
 | 
			
		||||
 | 
			
		||||
   from dependency_injector.wiring import Provide
 | 
			
		||||
   from dependency_injector.wiring import inject, Provide
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(bar: Bar = Provide[Container.bar]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -40,9 +42,10 @@ There are two types of markers:
 | 
			
		|||
 | 
			
		||||
.. code-block:: python
 | 
			
		||||
 | 
			
		||||
   from dependency_injector.wiring import Provider
 | 
			
		||||
   from dependency_injector.wiring import inject, Provider
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(bar_provider: Callable[..., Bar] = Provider[Container.bar]):
 | 
			
		||||
       bar = bar_provider()
 | 
			
		||||
       ...
 | 
			
		||||
| 
						 | 
				
			
			@ -51,18 +54,22 @@ You can use configuration, provided instance and sub-container providers as you
 | 
			
		|||
 | 
			
		||||
.. code-block:: python
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(token: str = Provide[Container.config.api_token]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(timeout: int = Provide[Container.config.timeout.as_(int)]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(baz: Baz = Provide[Container.bar.provided.baz]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(bar: Bar = Provide[Container.subcontainer.bar]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -100,6 +107,7 @@ When wiring is done functions and methods with the markers are patched to provid
 | 
			
		|||
 | 
			
		||||
.. code-block:: python
 | 
			
		||||
 | 
			
		||||
   @inject
 | 
			
		||||
   def foo(bar: Bar = Provide[Container.bar]):
 | 
			
		||||
       ...
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@
 | 
			
		|||
import sys
 | 
			
		||||
 | 
			
		||||
from dependency_injector import containers, providers
 | 
			
		||||
from dependency_injector.wiring import Provide
 | 
			
		||||
from dependency_injector.wiring import inject, Provide
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Service:
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +15,7 @@ class Container(containers.DeclarativeContainer):
 | 
			
		|||
    service = providers.Factory(Service)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@inject
 | 
			
		||||
def main(service: Service = Provide[Container.service]) -> None:
 | 
			
		||||
    ...
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@
 | 
			
		|||
import sys
 | 
			
		||||
 | 
			
		||||
from dependency_injector import containers, providers
 | 
			
		||||
from dependency_injector.wiring import Provide
 | 
			
		||||
from dependency_injector.wiring import inject, Provide
 | 
			
		||||
from flask import Flask, json
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -16,6 +16,7 @@ class Container(containers.DeclarativeContainer):
 | 
			
		|||
    service = providers.Factory(Service)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@inject
 | 
			
		||||
def index_view(service: Service = Provide[Container.service]) -> str:
 | 
			
		||||
    return json.dumps({'service_id': id(service)})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@
 | 
			
		|||
import sys
 | 
			
		||||
 | 
			
		||||
from dependency_injector import containers, providers
 | 
			
		||||
from dependency_injector.wiring import Provide, Closing
 | 
			
		||||
from dependency_injector.wiring import inject, Provide, Closing
 | 
			
		||||
from flask import Flask, current_app
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -22,6 +22,7 @@ class Container(containers.DeclarativeContainer):
 | 
			
		|||
    service = providers.Resource(init_service)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@inject
 | 
			
		||||
def index_view(service: Service = Closing[Provide[Container.service]]):
 | 
			
		||||
    assert service is current_app.container.service()
 | 
			
		||||
    return 'Hello  World!'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user