Replies: 12 comments 29 replies
-
|
Litestar will look great with the dishka |
Beta Was this translation helpful? Give feedback.
-
|
Да, дишка топчик. Спасибо Tishka) |
Beta Was this translation helpful? Give feedback.
-
|
Dishka’s a great pick—it just works, already plays nice with Litestar, and ditches string-based magic for clean, type-driven DI. |
Beta Was this translation helpful? Give feedback.
-
|
Sounds really nice! I would like to migrate from FastAPI, but when I read about DI in litestar, it looked not so good to me. Dishka sounds like the best option! |
Beta Was this translation helpful? Give feedback.
-
|
Dishka is best option |
Beta Was this translation helpful? Give feedback.
-
|
тащим дишку |
Beta Was this translation helpful? Give feedback.
-
|
I would consider abstracting a DI framework into a separate API a better alternative. I would prefer to have DI working based on names, yet providing type checking capabilities. For example: class UserController(Controller):
path = "/user"
dependencies = {"user": Provide(retrieve_db_user)}
@patch(path="/{user_id:uuid}")
async def get_user(self, user: User) -> User: ...with dishka would become: class UserController(Controller):
path = "/user"
@patch(path="/{user_id:uuid}")
async def get_user(self, user: FromDishka[User]) -> User: ...What if we need to inject two different "User" objects? I know this example is a bit out of this world with this naming, but could be applicable in other cases. For example, dependency-injector offers a way to explicitly inject a dependency from an IOC container: class UserController(Controller):
path = "/user"
@patch(path="/{user_id:uuid}")
@inject
async def get_user(self, user: User = Provide[SomeIOCContainer.user]) -> User: ...This way we know exactly which user we inject while keeping the type checking. |
Beta Was this translation helpful? Give feedback.
-
|
Punq is the best, dishka is too much, I don't need so complicated DI lib for DI =) |
Beta Was this translation helpful? Give feedback.
-
|
Dishka really cool choice to implement it here |
Beta Was this translation helpful? Give feedback.
-
|
Dishka x Litestar: rewriting the playbook. |
Beta Was this translation helpful? Give feedback.
-
|
All those who would like to see Dishka integrated, could you explain a bit more what specifically you like about dishka and what features you'd want from it in Litestar? |
Beta Was this translation helpful? Give feedback.
-
It is definitely not a dependency, but logic. Moreover, it often requires context data to work (for example, to check permission we might want to know which item is modified) In fastapi world the mix dependencies and just data processing or even request parsing. I do not think it is a good idea. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now
Litestarfeatures a DI based on names. There might be different opinions about it, but I would say that many people would prefer to have a DI based on types.I propose this idea:
v3will feature a new DI system based on typesv4orv5First alternative
We can create our own implementation. It is not really hard to do in the very basic form. Good reference example is
punq: https://github.com/bobthemighty/punqIt is basically a single file, quite easy to read and maintain.
Writing some basic functionality will be quite easy.
Second alternative
But, getting all the features right is quite hard. I would advocate for trying to ship a default integraion for some existing DI provider.
The best one is https://github.com/reagento/dishka
It supports sync and async providers, cleanups, scopes, etc. Basically anything that any real-world production app can require.
It already has a working integration with
Litestar: https://github.com/reagento/dishka/blob/develop/examples/integrations/litestar_app.pyBut, we can make it even easier by removing explicit
@injectdecorator requirement andsetup_dishkacall.Right now I would say that Dishka is already the most popular DI framework to be used with Litestar anyway.
CC @Tishka17
Third alternative
We can create an abstract API for using any DI you wish. It is the most complex and the most flexible solution.
We can call it "slots" DI.
I am open to hear opinions about it :)
Conclusion
This is a very early discussion about a very complex topic, please join and share your opinions / ideas :)
cc @provinzkraut
Beta Was this translation helpful? Give feedback.
All reactions