I recently decided to write my first web-app with Rust (as a Go/Java developer) using Actix::Web. I’m following use-case architecture. Here is the pseudocode to start the server:
let secrets = repository::secrets::Secrets::new();
let pool = Arc::new(DatabasePool::new(&secrets).await?);
let user_repository = Arc::new(UserRepository::new(Arc::clone(&pool)));
let user_management_usecase = UserManagementUsecase::new(Arc::clone(&user_repository));
let app_dependency = Arc::new(AppDependency::new(user_management_usecase);
And Then I pass app_dependency
to Actix HttpServer:
App::new()
.app_data(app_dependency.clone())
...
Overall, I create many use-cases
out of dependencies: APIs, repositories, utils, etc. And since I need to move app_dependency
into the future, I’m using Arc
to wrap all dependeinceis.
Somehow, I feel I’m abusing Arc
and not using it properly? Can you please advice what is the correct approach?
Note:
I tried to replace Arc
with lifetime, but I’m facing error: borrowed value does not live long enough
. I understand that I need to allocate structs on Heap, tho not sure what would be the best approach. I guess I cannot achieve this just using <'static>
. Reading further Rc
and Box
looks like wrong solution as well.