0

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.

2

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

0

In MVVM, think of the ViewModel (VM) as the "model" of the view. In simple words, this means that the VM is where you gather all your data/information/whatever that is required from the model (or from multiple models) and you transform this data to a format that the View understands. In principal, the view and the viewmodel are linked by the observer pattern it such way that the view reacts to a NotifyPropertyChanged signal sent by the VM. Similarly, the VM responds to signals from the Model in an observer structure. The VM subscribes to the M and the V subscribes to the VM. The View triggers procedures and accesses properties of the VM and, similarly, the VM accesses methods and properties in the Model.

In your example,

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;

Yes, these operations should be part of the model or different models.

and then the view model, which runs all the commands for that view.

In this case, the VM passes and retrieves whatever is necessary for the business logic and, perhaps, for other parts of the application.

However, I think that this service should only expose the list,

Yes but not only this

and that the viewmodel should be in charge of handling the item filtering

I don't know what you mean when you say to "be in charge of handling the item filtering". The retrieval of the filtered list from the database is the job of the model but the VM will pass the values of the filter to the model because the VM retrieves those values from the View. Then, the VM will transform the filtered list, if necessary, to a format that can be passed by the View.

Say for example, the list of your objects are customer names (first/last) and in a window you want to show the names in the following format: (first) & (last). If my name is John K, I want to show in the view "John & K".

The View does not concatenate the first and the last names; that would be view logic. The View just gets a string from the VM and knows where to show it in the view (form). It is the job of the VM to retrieve the names from the model (which gets them from the database or other sources) and to convert them to the appropriate format.

Of course, you can have more complex connections between the VM and several models but the principal is the same.

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.

The VM of the page with the list can be linked via the observer pattern to the VM of the confirmation page. When the use confirms the changes, the VM notifies the VM of the list page and, in turn, the VM notifies the View to update (reload) the list. This is the principal but you can come with a simpler approach here depending on who creates the confirmation page.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.