Salvo way of dependency injection #668
Replies: 5 comments 1 reply
-
For multithreaded contexts I use |
Beta Was this translation helpful? Give feedback.
-
You can just save data in your handler. |
Beta Was this translation helpful? Give feedback.
-
Any document said "withstate" the struct is copied only once per thread at the beginning of execution? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I finished to read the actix and axum documentation and don't mention copy once per thread. Actix: Setting Data Note that Data should be constructed outside the HttpServer::new closure if shared, potentially mutable state is desired. Data is cheap to clone; internally, it uses an Arc. See also App::app_data, Scope::app_data, and Resource::app_data. Axum: Whats the best approach to do something like this? (not the structure or wrapping function, I mean the best way to inject different services to the handlers) main: import (
) func main() {
} Register routes: func (server Server) registerAdminRoutes() {
} |
Beta Was this translation helpful? Give feedback.
-
Also, thanks for maintaining such a cool project! |
Beta Was this translation helpful? Give feedback.
-
Hey, what's the salvo way of managing dependency injection?
I have managed to do it with global OnceCell and with the Affix
With OnceCell it works but don't know clearly the effects in concurrency.
With affix is bad because the "AppState" struct (Or whatever you want to call the struct containing your services or any dependencies you want to inject) is being copied and dropped on each request
I have read that axum and actix, have "withstate" functions on the router, and understood that the struct is copied only once per thread at the beginning of execution.
What's the better way to manage it?
Beta Was this translation helpful? Give feedback.
All reactions