Segregation interfaces for ViewModels integration with DI #337
Answered
by
hmlongco
jordizspmobile
asked this question in
Q&A
-
|
Hi, I have a ViewModel that conforms to different interfaces: My module is the following: How can I retrieve this viewModel, for example, as ViewModelSectionAContract? Without having to cast it everywhere. Is there a way to retrieve this ViewModel with the same memory reference but as another protocol? |
Beta Was this translation helpful? Give feedback.
Answered by
hmlongco
Nov 20, 2025
Replies: 2 comments
-
|
You can either expose each individually... extension Container {
var viewModelA: Factory<ViewModelAContract> {
self { self.viewModel() }
}
var viewModelSectionAContract: Factory<ViewModelSectionAContract> {
self { self.viewModel() }
}
var viewModelSectionBContract: Factory<ViewModelSectionBContract> {
self { self.viewModel() }
}
var viewModel: Factory<ViewModelA> {
self { ViewModelA() }.shared
}
}Or pass the container... class TestContract {
let viewModel: ViewModelSectionAContract
init(container: Container) {
viewModel = container.viewModel()
}
}Or simply by assignment class TestContract {
var viewModel: ViewModelSectionAContract = Container.shared.viewModel()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
jordizspmobile
-
|
Perfect, thanks ^^ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can either expose each individually...
Or pass the container...
Or simply by assignment