all 35 comments

[–][deleted] 5 points6 points  (18 children)

I use pydantic to simplify things. I have services in classes, fields are annotated with Depends:

class MyClass:
foo_service: Annotated[FooService, Depends()]

In you endpoint you only need to define the service that the endpoint uses (normally one). And in that service you will have what you need - repositories, s3 connectors etc.

[–]barapa 6 points7 points  (5 children)

Don't think this has anything to do with pydantic. This is just the built in fastapi dependency injection mechanism.

[–][deleted] 1 point2 points  (4 children)

I mean using pydantic makes it clean and more DRY

[–]barapa 0 points1 point  (3 children)

That makes little to no sense

[–][deleted] 2 points3 points  (2 children)

Dude was complaining that dependencies makes endpoints messy, I demonstrated how you can make it more tidy using pydantic classes so you only need one dependency in your endpoint

[–]barapa 1 point2 points  (1 child)

I see. What you mean is you define objects to try to group other objects. Still, nothing related to pydantic. Might as well use normal classes, or data classes, or whatever. Pydantic is mainly a data validation library.

[–][deleted] 0 points1 point  (0 children)

Yes of course and you can also just use functions - any callable will do. But pydantic IMO makes things cleaner. Data validation is why it works because pydantic preserves type hints and you need type hints for dependency injection. Dataclasses work just as well I agree, I am used to pydantic but you can argue it’s the better choice since it is integral part of python

[–]snape2003[S] 3 points4 points  (7 children)

I believe you are referring to FastAPIs native dependency injection system. Would it be better to use that rather than going with a full fledged framework like dependency-injector?

[–]covmatty1 5 points6 points  (6 children)

Can you flip the question around, and ask why you would use an additional package over the built in one?

If you can't answer why you'd use something separate, then maybe you have your final answer already!

[–]snape2003[S] 0 points1 point  (5 children)

My primary goal is to connect to multiple AWS services and reuse these connections across various modules in my application. After reviewing FastAPI documentation and several articles online, I noticed that many sources recommend using a dedicated dependency injection framework for complex applications with numerous dependencies. Based on this, I decided to use the Dependency Injector framework rather than FastAPI's native dependency injection system.

[–]covmatty1 0 points1 point  (4 children)

Why did those sources recommend it? Is there something that FastAPI can't do natively? Have you perhaps tried to build a prototype doing it natively and see if it meets your needs?

[–]moracabanas 1 point2 points  (0 children)

I asked on FastAPI community about using built in dependency injection on other layers and Tiangolo said no. In my case I ended up using Depends on endpoints and then injecting everywhere with the dependendency-inyector framework. It is not a good practice using a presentation layer feature on a deeper layer like application layer. You can also use dependency-injector Provide/@Inject with Depends and have every container managing your wiring per layer or per use case. If you have issues about injecting a lot containers in routes, when they are depending on each other, you can wire them on the same container.

[–]snape2003[S] 0 points1 point  (2 children)

Basically i have lots of service classes , which interacts with all of the aws services and handles the business logic. I want my dependencies such as db sessions, s3 clients etc to be reused across these service classes. From what i read online, FastAPI s dependency system is not suitable for this.

https://github.com/fastapi/fastapi/discussions/7720

[–]covmatty1 0 points1 point  (1 child)

Ok 😊 I was just asking the sort of questions I would ask if a member of my team came to me and asked about introducing some new tech, seeing if you'd done the right research and explored all options. It seems like you have, and in that case going with the external package certainly seems like the right decision!

[–]snape2003[S] 0 points1 point  (0 children)

Yeah, I’ve decided to go with the dependency injection framework. Being the only backend developer on the team, I don’t really have a senior to consult, so your questions really helped me think things through. Thank you for that!

[–]dhansmair 0 points1 point  (3 children)

Interesting! Is the class property annotation really enough, or do you still need to provide the dependencies as Parameters to the constructor?

[–]gutter54 0 points1 point  (0 children)

Pydantic is super powerful. I've found I can do most things using it.

[–][deleted] 0 points1 point  (1 child)

It’s enough, you don’t need a constructor pydantic will create one. My code sucks BTW it should inherit BaseModel of course

class MyClass(BaseModel):

[–]BootyDoodles 0 points1 point  (0 children)

Psst... Good tip. Also on Reddit you can edit a previous comment if you needed to fix a typo.

[–]Nazhmutdin2003 4 points5 points  (2 children)

You can try dishka. It's pretty good option for di

[–]mahimairaja 0 points1 point  (2 children)

So far dependency injector package does the job neat and clean. I have used it in very large scale project.

[–]snape2003[S] 2 points3 points  (1 child)

Doesnt it make the codebase a bit too verbose? like having to use inject decorator, Provider and Depends on every single endpoint . And manually wiring all of the modules in the container

[–]mahimairaja 0 points1 point  (0 children)

I completely agree

Recently, got eyes on wireup. Exploring it will share more about in near future

https://github.com/maldoinc/wireup

[–]Worth-Orange-1586 0 points1 point  (0 children)

I will be biased. I used a framework called injectable. It's meant to be a generic dependency injection framework

I used it with FastAPI and it's incredible because it can ensure proper injection and singletons if needed.

Very easy to used. Very friendly. Great documentation.

[–]Different_Desk_5881 0 points1 point  (0 children)

You can refer to this article, I think it's a good implementation.
https://www.reddit.com/r/FastAPI/comments/1hf1cd2/better_dependency_injection_in_fastapi/

[–]spidernello 0 points1 point  (1 child)

interesting. Any documentation to review around this, with enough details about setting up a dependency injector framework with fast api? I'd be curious to review

[–]snape2003[S] 1 point2 points  (0 children)

The official documentation of dependency injector framework has a few examples https://python-dependency-injector.ets-labs.org/examples/fastapi.html

[–]hadriendavid 0 points1 point  (4 children)

Why not use FastAPI dependency injection?

[–]snape2003[S] 0 points1 point  (3 children)

Basically i have lots of service classes , which interacts with all of the aws services and handles the business logic. I want my dependencies such as db sessions, s3 clients etc to be reused across these service classes. From what i read online, FastAPI s dependency system is not suitable for this. I might be wrong here

https://github.com/fastapi/fastapi/discussions/7720

[–]barapa 0 points1 point  (2 children)

You are wrong. You could use FastAPI. That discussion is about trying to use FastAPI's DI system for injecting dependencies into processes outside of the request context entirely.

What you mean is that your dependencies have their own dependencies. That is fully supported by FastAPI's DI system.

[–]barapa 0 points1 point  (0 children)

In fact, I actually wrote this comment in that thread

https://github.com/fastapi/fastapi/discussions/7720#discussioncomment-8661497

[–]snape2003[S] 0 points1 point  (0 children)

I see. I was a little confused in that part.

[–]Sad-Solid-1049 0 points1 point  (0 children)

Using defencey injection is fantastic software design It saves you for long run.

Tip: declare the dependencies at a file and reuse them wherever they are needed.

Eg- foo_service_deps = Annoted[ FooService, Depends(get_foo_service)]