I want to know how would I go about architecting an MVC architecture using actix-web
I have a user_service
which requires user_repository
and this is how I’m currently doing it:
let config = Data::new(config.clone());
let mongo_repo = Data::new(MongoRepo::init(&config).await);
let user_repository = Data::new(UserRepository::new(&mongo_repo, &config));
let user_service = UserService::new(config_arc.clone(), user_repository.clone()).await;
then in main
:
HttpServer::new(move || {
let logger = middleware::Logger::default();
App::new()
.wrap(logger)
.wrap(middleware::NormalizePath::trim())
.app_data(web::FormConfig::default().limit(1 << 25))
.service(user_service::get_scope())
.default_service(web::to(|| HttpResponse::NotFound()))
})
.bind((host_address.to_owned(), port.to_owned()))?
.run()
.await
then in the user_service
:
pub struct UserService {
config: Data<Configuration>,
user_repository: Data<UserRepository>
}
impl UserService {
pub async fn new(config: Data<Configuration>, user_repository: Data<UserRepository>) -> Self {
UserService {
config: config,
user_repository
}
}
// ... other user service related functions
}
This feels a bit weird to me, is there a better way to achieve this?
I have previous experience with spring boot and .NET and dependency injection seems easier in those frameworks.
Is this how its supposed to be or is there any better way to do dependency injection?
Its working as expected but I want to know if this is the right way to do it or if there’s a different but easier way?