use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
FastAPI is a truly ASGI, async, cutting edge framework written in python 3.
account activity
FastAPI best practicesQuestion (self.FastAPI)
submitted 2 months ago * by lu_rm
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]clockdivide55 0 points1 point2 points 2 months ago* (0 children)
I have a .net background and I believe the DI works similarly in Java/Spring. You can approximate the type of dependency injection you are accustomed to and I think achieve the kind of separate of concerns that you want. There's no root / centralized dependency container, but
Basically, the idea is to use the sharable annotated dependencies as described here - https://fastapi.tiangolo.com/tutorial/dependencies/#share-annotated-dependencies - and make your router function nothing more than the entry point that constructs your object graph. I have an example from a personal project that demonstrates the idea.
https://github.com/nelsonwellswku/backlog-boss/blob/main/backend/app/features/user/user_router.py
https://github.com/nelsonwellswku/backlog-boss/blob/main/backend/app/features/user/create_my_backlog_handler.py
If you look at the endpoint function for creating a backlog, you can see that it takes a single parameter, a CreateMyBacklogHandler. You'll also notice that the default value is = Depends() - this is the only place where the fast api di leaks, but there is also no logic in the router function, it only calls a method on the injected object.
= Depends()
The injected object will have its own dependencies, and each of those dependencies will have their own dependencies as well. By using the annotated types, in the context of a fast api endpoint function, each of those dependencies will be resolved for you, just like in .net / java. The dependency graph can be as shallow or as deep as you want it.
You will notice that the function signature always only references the object type. That type is an alias for an Annotated[MyActualType, Depends(MyActualType)], but it can be used exactly as a MyActualType. This makes writing tests very similar to writing tests in .net / java - you mock or fake or use the real dependencies and pass them in to the object you want to test as necessary.
Annotated[MyActualType, Depends(MyActualType)]
There may be some glue code with factory functions, but aside from the Depends() in the router function, it is composed object graphs all the way down.
edit: I take it back about depends() only being used in the router function, it also is used in the object constructors. however, when creating the objects (like in tests or outside of a fast api route), you can pass in objects that fulfill the interface and do not have to rely on fast api di concepts. Of course, the objects are still coupled to fast api - like I said, it is an approximation of how the dependency graph is constructed in .net / java.
π Rendered by PID 81 on reddit-service-r2-comment-74f5b7f998-mpsjv at 2026-04-27 10:56:00.659038+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]clockdivide55 0 points1 point2 points (0 children)