@@ -61,11 +61,12 @@ When you call ``.shutdown()`` method on a resource provider, it will remove the
61
61
if any, and switch to uninitialized state. Some of resource initializer types support specifying custom
62
62
resource shutdown.
63
63
64
- Resource provider supports 3 types of initializers:
64
+ Resource provider supports 4 types of initializers:
65
65
66
66
- Function
67
- - Generator
68
- - Subclass of ``resources.Resource ``
67
+ - Context Manager
68
+ - Generator (legacy)
69
+ - Subclass of ``resources.Resource `` (legacy)
69
70
70
71
Function initializer
71
72
--------------------
@@ -103,8 +104,44 @@ you configure global resource:
103
104
104
105
Function initializer does not provide a way to specify custom resource shutdown.
105
106
106
- Generator initializer
107
- ---------------------
107
+ Context manager initializer
108
+ ---------------------------
109
+
110
+ This is an extension to the Function initializer. Resource provider automatically detects if the initializer returns a
111
+ context manager and uses it to manage the resource lifecycle.
112
+
113
+ .. code-block :: python
114
+
115
+ from dependency_injector import containers, providers
116
+
117
+ class DatabaseConnection :
118
+ def __init__ (self , host , port , user , password ):
119
+ self .host = host
120
+ self .port = port
121
+ self .user = user
122
+ self .password = password
123
+
124
+ def __enter__ (self ):
125
+ print (f " Connecting to { self .host} : { self .port} as { self .user} " )
126
+ return self
127
+
128
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
129
+ print (" Closing connection" )
130
+
131
+
132
+ class Container (containers .DeclarativeContainer ):
133
+
134
+ config = providers.Configuration()
135
+ db = providers.Resource(
136
+ DatabaseConnection,
137
+ host = config.db.host,
138
+ port = config.db.port,
139
+ user = config.db.user,
140
+ password = config.db.password,
141
+ )
142
+
143
+ Generator initializer (legacy)
144
+ ------------------------------
108
145
109
146
Resource provider can use 2-step generators:
110
147
@@ -154,8 +191,13 @@ object is not mandatory. You can leave ``yield`` statement empty:
154
191
argument2 = ... ,
155
192
)
156
193
157
- Subclass initializer
158
- --------------------
194
+ .. note ::
195
+
196
+ Generator initializers are automatically wrapped with ``contextmanager `` or ``asynccontextmanager `` decorator when
197
+ provided to a ``Resource `` provider.
198
+
199
+ Subclass initializer (legacy)
200
+ -----------------------------
159
201
160
202
You can create resource initializer by implementing a subclass of the ``resources.Resource ``:
161
203
@@ -263,10 +305,11 @@ Asynchronous function initializer:
263
305
argument2 = ... ,
264
306
)
265
307
266
- Asynchronous generator initializer:
308
+ Asynchronous Context Manager initializer:
267
309
268
310
.. code-block :: python
269
311
312
+ @asynccontextmanager
270
313
async def init_async_resource (argument1 = ... , argument2 = ... ):
271
314
connection = await connect()
272
315
yield connection
0 commit comments