简体   繁体   English

在Windows Phone 7的MVVM应用程序中使用REST Web服务的良好体系结构?

[英]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. 我在确定将数据从Web服务获取到UI的最佳方法时遇到了麻烦。

Given the asynchronous nature of WebClient how would you build this? 鉴于WebClient的异步特性,您将如何构建它?

  • Model uses WebClient to talk to webservice 模型使用WebClient与Web服务对话
  • ViewModel asks model for data ViewModel向模型询问数据
  • View is databound to ViewModel 视图数据绑定到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. 在异步完成事件上,我需要将数据从模型中返回到ViewModel,这是我考虑过的事情。

  1. I could fire an event in the Model that the ViewModel subscribes to. 我可以在ViewModel订阅的模型中触发一个事件。
  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? 还是应该在ViewModel和Model之间做第二级INotifyPropertyChanged事件?
  4. Or am I very confused and completely misunderstanding MVVM? 还是我很困惑并且完全误解了MVVM?

It depends on how purist you want to be about MVVM. 这取决于您对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). 您可以将API本身视为模型,在这种情况下,ViewModel具有WebClient,并且在Async完成后,您可以设置属性(它们将依次从其设置器中触发PropertyChanged)。

Or you can have a local Model that has the WebClient code in it (as it sounds like you have). 或者,您可以在其中包含WebClient代码的本地模型(听起来像您已经拥有)。 In this case, my personal approach would to have a "ModelUpdated" event that fires from the Async complete event. 在这种情况下,我个人的方法是有一个“ ModelUpdated”事件,该事件从Async complete事件中触发。 (Your option 1). (您的选项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. 您的ViewModel可以侦听此事件,并可以触发PropertyChanged(null)以使View询问所有属性,或者触发多个PropertyChanged事件。 Remember you're not restricted to firing PropertyChanged from within your setters. 请记住,您不限于在设置器中触发PropertyChanged。 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. 因此,您可以在模型完成填充时调用该方法,并且您的用户界面将在激发每个属性时调用update。 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) . 仅当您拥有大量属性并且不想一次使用PropertyChanged(null)触发它们时,才需要执行此操作。

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. 通常,我将相关服务传递给ViewModel,然后ViewModel进行异步调用的处理,并显示繁忙状态和所有有趣的东西。

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). 例如,如果您有一个Product Model,一个ProductListViewModel和一个产品集合以及一个搜索命令,那么您将引入一个ProductSearchService(或加载所有产品的ProductLoadService)。 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. 然后,我将那个ProductSearchService传递到您的ProductListViewModel构造函数(依赖项注入)中,并让您的ViewModel通过调用相关的服务方法并加载响应来控制对Products(您的模型对象)的检索。

  • ProductListService Returns Product (Model) List ProductListService返回产品(型号)列表
  • ProductListViewModel uses ProductListService to get Products ProductListViewModel使用ProductListService获取产品
  • ProductListView binds to ProductList ObservableCollection in ProductListViewModel. ProductListView绑定到ProductListViewModel中的ProductList ObservableCollection。

This pattern essentially resembles Model-View-Controller, where the ViewModel takes more of the Controller responsibilities. 这种模式实质上类似于Model-View-Controller,其中ViewModel承担更多Controller职责。

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 因为您提到了基于REST的Web服务,所以我有一个示例博客文章,其中使用MVC 2 JSON结果作为Win Phone 7应用程序的服务层: 具有MVC 2 JSON服务的数据驱动Win Phone 7应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM