Distinguishing the differences between view model and service layer

mvvmservicexamarin

First project trying to use MVVM and Xamarin Forms, so please be gentle; however, I am at a lost on how to differentiate between the logic that belongs in the view model and model/service layer.

I am working on a simple app that shows a list, has a detail page for the list object when selected to change one field, and then return to the list. Once the user is satisfied, they navigate to another page that can confirm the transactions, and sends this to the on phone database and the connected web service.

Currently, I have 3 services: web service, database service, and itemcount service. I then have 3 view models, with their respective views: itemcountpage (this shows the whole list), itemcount detail page, and the itemcount confirmation page.

Where I am becoming lost is the interaction of the itemcount service, which holds all the logic for this process, i.e. getting my list from the db/web service, filtering the list, and then updating data in the database/web service; and then the view model, which runs all the commands for that view.

However, I think that this service should only expose the list, and that the viewmodel should be in charge of handling the item filtering and letting the service know which items need to be updated and should be shown in the next view, the itemcount confirmation view where all the changed items should be shown. I do not know where/when to use the observablecollection or notifypropertychanged essentially as this will be changing quite a lot.

I keep flipping back and forth on which layer this goes into, service or viewmodel, and would like a little direction. If there are also any great resources that you may have used to learning MVVM I would be eternally greatful.

Best Answer

The view model acts as an intermediary between the view and the model, and is responsible for handling the view logic.

The model in the MVVM pattern encapsulates business logic and data. Business logic is defined as any application logic that is concerned with the retrieval and management of application data and for making sure that any business rules that ensure data consistency and validity are imposed

Have a look at the following link:

https://msdn.microsoft.com/en-us/library/hh848246.aspx

you can also have a look at the following link that takes an in depth look at the MVVM pattern.

https://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx