Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good architecture for consuming a REST web service in an MVVM app for Windows Phone 7?

I'm having a bit of trouble deciding on the best way to get the data from the web service to the UI.

Given the asynchronous nature of WebClient how would you build this?

  • Model uses WebClient to talk to webservice
  • ViewModel asks model for data
  • View is databound to ViewModel

On the Async complete event I need to get that data back out of the model to the ViewModel, these are the things I've thought about.

  1. I could fire an event in the Model that the ViewModel subscribes to.
  2. I could perhaps do something with passing around callbacks?
  3. Or should I be doing a second level of INotifyPropertyChanged events between the ViewModel and the Model?
  4. Or am I very confused and completely misunderstanding MVVM?
like image 216
Will Avatar asked Jul 05 '10 21:07

Will


People also ask

Why Using MVVM Android?

MVVM separates your view (i.e. Activity s and Fragment s) from your business logic. MVVM is enough for small projects, but when your codebase becomes huge, your ViewModel s start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases.

Why Using MVVM?

The MVVM pattern presents a better separation of concerns by adding view models to the mix. The view model translates the data of the model layer into something the view layer can use. The controller is no longer responsible for this task.

Is MVVM good?

In short: MVVM is not pointless, it's great. NET 4.0 WPF's control library is trash. Here is the simple proof of concept ViewModel which you can't data bind in pure MVVM manner using WPF.

What is VM in MVVM?

SUMMARY: From Server, Get Data(available in Model Objects), View Model reads Model Objects and then facilitates the easy presentation of data on the view. The primary differences between MVVM AND MVC are as follows: MVVM. MVC.


2 Answers

It depends on how purist you want to be about MVVM.

You could treat the API itself as your Model, in which case the ViewModel has the WebClient and on Async completed you'd set your properties (and they would in turn fire PropertyChanged from within their setters).

Or you can have a local Model that has the WebClient code in it (as it sounds like you have). In this case, my personal approach would to have a "ModelUpdated" event that fires from the Async complete event. (Your option 1).

Your ViewModel can listen for this event, and either fire a PropertyChanged(null) to have the View ask for ALL properties, or fire multiple PropertyChanged events. Remember you're not restricted to firing PropertyChanged from within your setters. There's nothing stopping you from having a method like

private void FireMultipleProperties(){
NotifyPropertyChanged("Property1");
NotifyPropertyChanged("Property2");
NotifyPropertyChanged("Property3");
}

So you could call that method when the Model finishes populating, and your UI will call update each property when they are fired. You'd only need to do this if you have a ton of properties and don't want to fire them all at once with PropertyChanged(null).

like image 50
Ben Gracewood Avatar answered Oct 01 '22 08:10

Ben Gracewood


I think you need to introduce a new layer into your architecture; a service layer. Usually, I pass in my relevant services to my ViewModel and the ViewModel does the handling of the async calls and showing busy states and all that fun stuff.

For instance, if you have a Product Model, and ProductListViewModel with a collection of products and maybe a search command, then you would introduce a ProductSearchService (Or ProductLoadService that loads all products). I would then pass that ProductSearchService into your ProductListViewModel constructor (dependency injection) and let your ViewModel control the retrieval of the Products (your model objects) by calling the relevant service methods and loading the response.

  • ProductListService Returns Product (Model) List
  • ProductListViewModel uses ProductListService to get Products
  • ProductListView binds to ProductList ObservableCollection in ProductListViewModel.

This pattern essentially resembles Model-View-Controller, where the ViewModel takes more of the Controller responsibilities.

Since you mention REST based web services, I have an example blog post of using MVC 2 JSON results as a service layer for a Win Phone 7 app: Data Driven Win Phone 7 Apps with MVC 2 JSON services

like image 21
Jacob Avatar answered Oct 01 '22 08:10

Jacob