CleanArchitectureRxSwift icon indicating copy to clipboard operation
CleanArchitectureRxSwift copied to clipboard

Issues with the current ViewModel implementation.

Open jdisho opened this issue 6 years ago • 2 comments

Hi Serg,

First of all, I want to thank you for the great explanation about CleanArchitecture, but I am a bit concerned about the way the ViewModel is created. Based on this approach, if a change is made in the Input, the transform function will be called and all the outputs will be recreated again. This is not necessarily right.

extension ViewModel: ViewModelType {

    struct Input {
        ...
    }

    struct Output {
        ...
    }

    func transform(input: Input) -> Output {
        ...
        return Output(output: someOutput)
    }

}

What if we create the ViewModel like this, in a protocol oriented way:

protocol ViewModelInput {
    ...
}

protocol ViewModelOutput {
    ...
}

protocol ViewModelType {
    var input: ViewModelInput { get }
    var output: ViewModelOutput { get }
}

extension ViewModel: ViewModelInput, ViewModelOutput, ViewModelType {

   var input: ViewModelInput { return self }
   var output: ViewModelOutput { return self }

   ...
}

The ViewModel is easier to be tested and the outputs aren't recreated for every change in Input.

What do you think?

jdisho avatar Feb 06 '18 10:02 jdisho

@sergdort Anything?

jdisho avatar Feb 21 '18 00:02 jdisho

Hi @jdisho

Not sure what do you mean by:

if a change is made in the Input, the transform function will be called and all the outputs will be recreated again.

I believe that in the example app Input is just a struct that contains user input observables

sergdort avatar Feb 22 '18 10:02 sergdort