CleanArchitectureRxSwift
CleanArchitectureRxSwift copied to clipboard
ViewModelType protocol and associatedtype limitations
Should't PostsViewModel be an protocol and not class? I think keeping protocol oriented view models is better than enforcing generic structure with associatedtype. Input/output transform can always be enforced per viewModel protocol. Thoughts?
Hi, @IvanKalaica, thanks for the question :)
What benefits do you think we would get from using protocol
? The only thing that comes to mind is snapshot and UITests.
Input/output transform can always be enforced per viewModel protocol.
Curious to see an example :)
I think interface based programming is always a good habit. :)
struct ExampleViewModelInput {
}
struct ExampleViewModelOutput {
let speedInKmh: Observable<Int>
}
protocol ExampleViewModel {
func transform(input: ExampleViewModelInput) -> ExampleViewModelOutput
}
final class DefaultExampleViewModel: ExampleViewModel {
private let exampleService: ExampleService
init(exampleService: VehicleService) {
self.exampleService = exampleService
}
}
extension DefaultExampleViewModel {
func transform(input: ExampleViewModelInput) -> ExampleViewModelOutput {
return ExampleViewModelOutput(
speedInKmh: self.exampleService.map { $0.speedInKmh }.distinctUntilChanged()
)
}
}
@IvanKalaica Could you explain more why the below code is good habit?
@sergdort as per Dependency Inversion Principle (DIP) when 2 classes or modules are communicating with each other than both should link to abstraction rather than knowing about concrete module/class, which makes the system loosely coupled.