Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MVVM service layer

Tags:

mvvm

wpf

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.

like image 461
Flack Avatar asked Aug 30 '10 18:08

Flack


People also ask

What is a service in WPF MVVM?

Services provide a specific UI-aware functionality for Views in MVVM applications. Although services are defined within Views, their functionality can still be invoked from View Models that may not even include information about Views.

Where is business logic in MVVM?

ViewModel: ViewModel is the middle layer between the view and model. ViewModel contains the business logic, which manipulates the row data to show in the view. Any kind of function and methods should be in the view model. The iNotifyPropertyChanged interface is used in the ViewModel to achieve two-way binding.

What does MVVM stand for?

Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls. MVVM is also known as model-view-binder and was created by Microsoft architects Ken Cooper and John Gossman.


Video Answer


2 Answers

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.

like image 99
Daniel Auger Avatar answered Oct 04 '22 09:10

Daniel Auger


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.

like image 33
HCL Avatar answered Oct 04 '22 08:10

HCL