https://github.com/lexxai/fastapi_depends
FastAPI Dependency Injection: 21 Examples Using Depends() with AI Answers
https://github.com/lexxai/fastapi_depends
ai dependency-injection docker fastapi patterns postgresql python redis singleton sql sqlalchemy
Last synced: 3 months ago
JSON representation
FastAPI Dependency Injection: 21 Examples Using Depends() with AI Answers
- Host: GitHub
- URL: https://github.com/lexxai/fastapi_depends
- Owner: lexxai
- Created: 2024-10-18T14:33:33.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-22T19:17:26.000Z (over 1 year ago)
- Last Synced: 2025-04-05T16:13:05.634Z (about 1 year ago)
- Topics: ai, dependency-injection, docker, fastapi, patterns, postgresql, python, redis, singleton, sql, sqlalchemy
- Language: Python
- Homepage: https://lexxai.blogspot.com/2024/10/fastapi-dependency-injection-21.html
- Size: 104 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FastAPI Dependency Injection: 21 Examples Using Depends() with AI Answers
## Why ?
Once I wanted to understand how Depends works in FastAPI more thoroughly to understand what's under the hood. I started communicating with the AI. The answer was a question, and I got hooked.
I decided to test it in real code and created a training project and wrote this post for it based on my questions to the AI and its answers.
Some answers were wrong, some I had already optimized a bit. But all the results shown are real and created by me personally.
The result was 21 training examples written in Python, where I checked the answers and suggestions from the AI.
Full publication:
https://lexxai.blogspot.com/2024/10/fastapi-dependency-injection-21.html
Deployed version:
https://fastapi-depends.onrender.com/docs
Questions:
- Prepare for use FastAPI project
- How does injection Depends work in FastAPI?
- The decision of who will run when depends on the finish route. The question is, will the yield continue?
- But the response was wrong!! We were expecting the output "Cleaning up the resource" after the error.
- So the dependency function must always be used with a try: finally?
- Okay, if depends is a class, then its instance is created once, like a singleton? How can information be saved between requests if the instance is dead after the request?
- In the redis example, should the database initialization ideally also be in the form of dependencies?
- With the asynchronous yeld for the asynchronous database initialization, is there a lot of complexity involved?
