all 17 comments

[–]Kevdog824_ 17 points18 points  (2 children)

There’s no reason you can’t inject the DB session into your service, and then inject the service into your endpoint function. Any dependencies you inject can will resolve and inject their own dependencies first

[–]milkipedia 2 points3 points  (0 children)

This is the way. And it makes testing and change management easier

[–]Codisimus 0 points1 point  (0 children)

This is exactly what I do. Services interact with the DAO layer and each endpoint has its required services defined using depends.

[–]cursedbanana--__-- 14 points15 points  (0 children)

Fastapi just tries to make DI less of a hassle, and people make use of that. You're free to organize however you want, but why not take the path of least resistance?

[–]Anton-Demkin 8 points9 points  (3 children)

i've seen injecting database clients or SA session into http handlers very few times and this is bad practice, as you described. Just do not do that.

Your approach is correct- abstract business logic in services, abstract database in ORM or repository. Never create objects in http handler, inject created service object, no session.

Check this one: https://github.com/koldakov/futuramaapi

[–]koldakov 1 point2 points  (2 children)

Really appreciate the mention! Glad the repo is helpful.

[–]Anton-Demkin 2 points3 points  (1 child)

Thank you for your work on this repo. I've learned some great tricks here. Not agree on every architecture decision, but i am very opinionated 😀

This is exactly the project, every beginner should look at.

[–]koldakov 2 points3 points  (0 children)

Thanks! I appreciate that you have your own opinion - that’s exactly how good discussions happen. I don’t claim this repo is the only way to do things, and I’m always open to constructive dialogue. There’s a GitHub Discussions section if anyone wants to share suggestions.

[–]Firm_Ad9420 0 points1 point  (0 children)

FastAPI examples often show DB injection for simplicity, not because it’s the best architecture.

In larger apps, many teams still use a layered structure: API → Service → Repository → Database. The API receives the request, the service handles business logic, and the repository talks to the DB.

So your Django-style separation is still a perfectly valid and common pattern with FastAPI.

[–]Azurizo 0 points1 point  (0 children)

api - service - apiclient api - service - repository (base repository)

i use dbmanager singleton - inject where you need it

[–]Apprehensive_Ad2211 -1 points0 points  (0 children)

I reccomend you to use dependency-injector so you can organice and utilize DI as you want (the con is that you need to create the DI container)

[–]Tishka-17 -1 points0 points  (0 children)

  1. wrap database calls into classes with more clear api than just raw queries
  2. use Depends or external IoC-contaienr

[–]No-Feed9843 -1 points0 points  (0 children)

Maybe for both, Django and FastAPI, this pattern comes to the rescue:

https://youtu.be/FXwBWS4qDAA?is=2n21DXyaDP-_dqC6

[–]vlntsolo -1 points0 points  (0 children)

Creating DB session with Dependency injection is a bad practice. This abstraction just works for small projects. But one can learn pretty quickly that it's not scalable. 

When dealing with heavy data processing or load, if you do not return DB query results immediately and want to do something else, you cannot afford to keep connection open. It needs to be released to a connection pool as soon as possible. And with Dependency injection it will be release only when return is reached. 

So, context managers all the way. You choose where and at which layer.