r/FastAPI • u/reveliscano • Dec 10 '25
Question Using dependency_overrides for global composition?
I have a project built around a Hexagonal Architecture. FastAPI is just one input adapter there.
The FastAPI adapter only depends on a Repository. It doesn't care or know which actual technology is dealing with the persistency.
I have a main.py file which serves as the composition root. It instantiates concrete dependencies and wire them together. Thus, I need a way that main.py is able to inject the concrete Postgres adapter for FastAPI.
The only thing that I've found to make this possible is dependency_overrides. But its docstring mentions that it's meant for testing. What do you think? Might it be better using a DI framework?
View all comments
Show parent comment
1
u/reveliscano Dec 15 '25
Thanks! It's good to know somebody else is doing that.
I think I will have to switch to a DI framework sooner than later, tho. As I said in another comment, a Cron adapter will need to be added soon (Celery or Temporal). I guess it will need something to inject the depencies. And it will be weird to have two mechanisms for Dependency Injection within the same project.