#Dependency Injection Questions

1 messages · Page 1 of 1 (latest)

distant drift
#

Hey guys, I have a question about the usage of DI. I come from the java & SpringBoot world but looking at the documentation and testing it myself, it seems to me that it works quite differently. My "java developer" instinct is to create a class and inject it into let's say a controller.
It seems to be instantiating the dependency every time unless I use use_cache=True , so essentially my dependency class is not a true singleton(I guess this concept is not really a thing in python).
I want to eagerly initialize the dependency class. It seems to be initializing it when I make my first API call when it needs to invoke it. The issue with lazy initialization is that my init method needs to load a bunch of files to memory and the server shouldn't really be considered running until this is done.

Looking at the above points, what is the practice you recommend? This is a pretty basic use-case of having a controller class for the api logic and a service class for the actual processing and business logic. Should I drop the whole "service class" approach and just create static methods and DI the processing method instead of the whole class?

burnt warrenBOT
#
Notes for Dependency Injection Questions
At your assistance

@distant drift

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

placid depot
#

It seems to be instantiating the dependency every time unless I use use_cache=True , so essentially my dependency class is not a true singleton(I guess this concept is not really a thing in python).
It kinda is, but it's scoped to the request

#

All DI in Litestar (currently) is tied to the request lifecycle. The reason for that is simply that it started out as a convenient way to provide/inject things that depends on the request, e.g. path parameters, query parameters, headers, etc., and things that might depend on those, like a user object or a datbase connection

#

The issue with lazy initialization is that my init method needs to load a bunch of files to memory and the server shouldn't really be considered running until this is done
If you want something to run before the server starts, there's two things you can use: The lifespan and server_lifespan parameters on the Litestar instance. They both allow you to run some code on startup/shutdown, with the difference being that lifespan is invoked by the ASGI server on startup, meaning that if you run e.g. uvicorn with 2 processes, it will be called for each process. server_lifespan only works when using the litestar CLI (litestar run) and does the same thing, except it only gets invoked once before the server itself starts

#

You can combine those (lifespan) is probably what you want, with application state, where you can store arbitrary stuff, which you can then inject via DI

distant drift
#

Really appreciate your help!

distant drift
#

!solve