MVVM 中数据应该存储在哪里?
我有这个使用 MVVM 的 Silverlight Prism 应用程序。该模型调用 WCF 服务并返回数据列表。
ViewModel 绑定到 View,因此 ViewModel 应该有一个 List 属性。
我是否应该将 WCF 服务返回的数据保留在 MVVM 中?
List 属性是否应该使用其 getter 调用模型?其中模型具有 ReturnListOfData() 方法,该方法返回模型中存储的数据。
或者 ViewModel 是否在 Model 调用服务器完成后存储数据?
这是在哪里对 MVVM 中的 WCF 或其他 Web 服务进行调用? 的后续内容
I've got this Silverlight Prism application that is using MVVM. The model calls a WCF service and a list of data is returned.
The ViewModel is bound to the View, so the ViewModel should have a List property.
Were should I keep data returned by a WCF service in MVVM?
Should the List property be calling into the Model using its getter? Where the model has a ReturnListOfData() method that returns the data stored in the model.
Or does the ViewModel stores the data after the Model is done with calling the server?
This is a follow up on Where to put the calls to WCF or other webservices in MVVM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,如果我需要保留模型对象(我认为从 WCF 服务返回的大多数东西都是模型对象),我会将其存储在我的 ViewModel 中的“模型”属性中。
我见过人们甚至在他们的基本 ViewModel 类型上创建一个标准的 Model 属性,就像这样(我不这样做,但它很好):
这真的取决于你。让它们尽可能靠近相关的 ViewModels 可能是这里需要考虑的事情。
Generally if I need to keep the Model objects around (I consider most things coming back from a WCF service a Model object) I will store it in my ViewModel in a "Model" property.
I've seen people go so far as to create a standard Model property on their base ViewModel type, like this (I don't do this, but it's nice):
It's really up to you. Keeping them as close to their related ViewModels is probably the thing to take away here.
这实际上取决于您应用程序的其他方面。例如,ReturnListOfData() 返回的数据如何使用?还有其他组件对此感兴趣吗?用户是否更新列表中的元素?它可以创建他稍后想要保存的新元素吗? 。
在最简单的情况下,您只需视图模型公开一个 List 属性来查看,然后将该列表重置为 ReturnListOfData() 返回的任何内容 它可能适用于以下情况:用户仅执行搜索,稍后不对数据执行任何操作,并且只有一个视图对该数据感兴趣。
但是假设用户希望能够修改该列表的元素。显然,您必须以某种方式跟踪原始列表中的更改,因此当用户单击保存(或取消)时,您将仅向服务器发送已更改(或添加)的元素,或者如果用户点击取消。在这种情况下,您需要一个 Model 对象,它将保留原始数据,因此您的视图模型仅包含其副本。
It really depends on other aspects of your application. E.g. how's the data returned by ReturnListOfData() used? Are there other components interested in it? Does user update elements in the list? Can it create new elements that he'll want to save later? etc.
In the simplest case you'd just have a List property exposed by your viewmodel to view, and you'd reset that list to whatever ReturnListOfData() returned. It will probably work for a case when user simply performs a search, doesn't do anything to the data later on, and there's only one view that is interested in that data.
But suppose a user wants to be able to modify elements of that list. Clearly, you'll have to somehow track the changes in that original list, so then when user clicks save (or cancel), you'd send to the server only elements that were changed (or added) or restore the original elements if user clicks cancel. In this case you'd need a Model object, that would keep the original data, so then your viewmodel contains only its copy.