8
votes

I plan on writing a WPF app following the MVVM pattern for the first time but something is not quite clear to me. Let's say that the view has a "Save" button and when that is hit I need to save the current state of my data (the model). This will be done by sending a SOAP message to a SOAP service.

Where in my MVVM setup do these SOAP request/response handlers live? Does the view model make the SOAP call itself whenever the save button is hit? Should the view model notify the model to save itself instead? Maybe it's some other way, completely separate from the MVVM?

My thinking was that (at least in this specific case) the view model would handle it since it needs to disable the save button in the view until the current save request has completed.

3
I started writing an answer, and then talked myself out of it, and +1 instead. My instinct is that the Model should handle it, since most ViewModels use a DataContext of the Model, but this isn't really updating the Model itself, but rather something outside that Model...Wonko the Sane

3 Answers

5
votes

I typically put a logical client-side application/business layer between the viewmodel and the SOAP/WCF/Webservice layer. This layer is where all the non-view business logic and processing logic lives. Remember, the viewmodel is the model of the view, not the model of the domain. Therefore, you want to hand off control to the next layer down ASAP.

In this scenario, I would have the view trigger a save command on the the viewmodel, which would in turn call into the application layer, which would in turn make any calls to remote services.

4
votes

The ViewModel, should not do such an operation. It only should trigger it. Therefore the model has to do it (or another intermediate layer that is responsible for the load-and save-operations, but not the ViewModel itself).

The ViewModel can observe the save-operation and may provide state-information about the progress for the View.

0
votes

I would create a service handler that can be accessed by the ViewModel. Pass this into the constructor of the viewmodel, and call the methods exposed by the service handler.