You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3deb7e2
Browse filesBrowse the repository at this point in the historyBrowse files
This will enable you to use dependency injection in your `FastAPI` endpoints:
60
60
61
-
> **Note**: If you don't want to use the custom router class, you can make use of`fastapi.Depends` instead.
61
+
> If you don't want to use the custom router class, you can use `fastapi.Depends` instead.
62
62
63
63
64
64
=== "Router class"
@@ -90,7 +90,7 @@ This will enable you to use dependency injection in your `FastAPI` endpoints:
90
90
91
91
### Managing container context
92
92
93
-
If you wish to initialize the container context you can simply pass arguments to `create_fastapi_route_class()`:
93
+
To initialize the container context, pass arguments to `create_fastapi_route_class()`:
94
94
95
95
```python
96
96
from that_depends import ContextScopes
@@ -103,16 +103,16 @@ and the global context will be set.
103
103
104
104
105
105
106
-
## Integrating with FastAPI Using DIContextMiddleware
106
+
## Integrating with FastAPI using DIContextMiddleware
107
107
108
108
The `DIContextMiddleware` can be used to manage context, but its features overlap with the [custom router class](#using-a-custom-router-class).
109
109
The main advantage of using middleware is that you can set it up for your entire `FastAPI` application.
110
110
111
-
> **Note:**If you want to use both the `DIContextMiddleware` and the custom router class, you should not pass any arguments to `create_fastapi_route_class()`.
111
+
> If you want to use both the `DIContextMiddleware` and the custom router class, do not pass any arguments to `create_fastapi_route_class()`.
112
112
113
-
### Setting Up the FastAPI App
113
+
### Setting up the FastAPI app
114
114
115
-
You can use **`that-depends`**’s `DIContextMiddleware` so that any request automatically initializes the context for your container(s). This approach is convenient if you want to:
115
+
You can use the `DIContextMiddleware` from `that-depends` so that any request automatically initializes the context for your container(s). This approach is convenient if you want to:
116
116
117
117
- Automatically initialize or tear down resources on each request.
118
118
- Provide a global or request-level context dictionary you can read from your container.
@@ -147,7 +147,7 @@ def get_time(
147
147
)
148
148
```
149
149
150
-
-**`DIContextMiddleware`** automatically sets a “global context” for every request.
150
+
-`DIContextMiddleware` automatically sets a global context for every request.
151
151
- The `Depends(MyContainer.current_time)` call is how you reference the container’s provider using the standard FastAPI injection system.
152
152
153
153
To run this app:
@@ -158,15 +158,15 @@ uvicorn main:app --reload
158
158
159
159
When you make a request to `/`, you will see the current time printed, and behind the scenes the that-depends container is in a context.
160
160
161
-
> **Note**: If your container uses advanced context-based resources (e.g. `ContextResource`), you may also set `default_scope` in your container, or configure an explicit scope. See the advanced section below.
161
+
> If your container uses context-based resources such as `ContextResource`, you can also set `default_scope` in your container or configure an explicit scope. See the advanced section below.
For “request-scoped” resources (e.g. a DB connection per request), you can use `ContextResource` in your container. This typically works in conjunction with `DIContextMiddleware` or a manual `container_context(...)` call. For example:
201
201
@@ -247,7 +247,7 @@ async def read_db(
247
247
248
248
When writing unit tests, you can use `TestClient` from `starlette.testclient` or `pytest-asyncio` with standard FastAPI patterns. The `DIContextMiddleware` approach ensures resources are created and torn down automatically each request, so no special arrangement is necessary.
1.**Global vs. Request Context**: Decide whether your container’s dependencies should be globally shared (e.g., singletons) or created anew per request (e.g., database or session).
274
-
2.**Combining with FastAPI’s `Depends`**: Generally, you can pass `Depends(MyContainer.some_provider)` to route handlers. Under the hood, that-depends will be invoked.
275
-
3.**Overriding**: You can override a provider in tests by calling `MyContainer.some_provider.override_sync(...)` or using the context manager `with MyContainer.some_provider.override_context_sync(...):`.
276
-
4.**Performance**: If you have expensive creation logic (like a DB engine that can be reused globally), prefer using a `Singleton` or `Object` provider. If you need ephemeral resources, use `ContextResource` with the `DIContextMiddleware`.
277
-
5.**Custom Context**: If you do not want to rely on the middleware, you can manually create a context in any async function by calling `async with container_context(MyContainer):`.
278
-
6.**Multiple Containers**: You can define multiple containers and connect them (e.g., `ContainerA.connect_containers(ContainerB)`), or add them all to the `DIContextMiddleware`. For advanced usage, see the that-depends documentation on “container connection.”
273
+
-Decide whether your container's dependencies should be globally shared (e.g., singletons) or created anew per request (e.g., database or session).
274
+
- You can generally pass `Depends(MyContainer.some_provider)` to route handlers, and `that-depends` resolves the provider under the hood.
275
+
- You can override a provider in tests by calling `MyContainer.some_provider.override_sync(...)` or using the context manager `with MyContainer.some_provider.override_context_sync(...):`.
276
+
-If you have expensive creation logic (like a DB engine that can be reused globally), prefer a `Singleton` or `Object` provider. If you need ephemeral resources, use `ContextResource` with the `DIContextMiddleware`.
277
+
-If you do not want to rely on the middleware, you can create a context manually in any async function by calling `async with container_context(MyContainer):`.
278
+
-You can define multiple containers and connect them (e.g., `ContainerA.connect_containers(ContainerB)`), or add them all to the `DIContextMiddleware`. See [Usage with multiple containers](../introduction/multiple-containers.md) for details.
279
279
280
280
281
-
### Accessing the FastAPI Request or Other Context Items
281
+
### Accessing the FastAPI request or other context items
282
282
283
283
Sometimes you want to pass the `fastapi.Request` (or other request-scoped data) into the container context so that providers can read it. You can do that either via the `DIContextMiddleware` (by customizing the `global_context` dynamically) or by writing your own dependency that calls `container_context()`.
284
284
285
-
**Example**: Writing a custom dependency that sets up the context with the current `Request`:
285
+
For example, a custom dependency that sets up the context with the current `Request`:
0 commit comments