Model从哪里获取MVVM中的数据? [英] From where does Model get data in MVVM?

查看:158
本文介绍了Model从哪里获取MVVM中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究MVVM模式。我很清楚Model包含类的概念,View只是数据的可视化表示,ViewModel是连接Model和View并执行所有操作的层。



但是我还不清楚模型从何处获取数据?因为它只包含属性。因此,即使我们使用WCF来填充数据,问题仍然存在:



是否需要在模型中填写数据或执行此项工作的任何中间类通过ViewModel?



Model为ViewModel提供数据,ViewModel又为View和View提供格式化数据,只代表数据。但是模型从哪里获取数据?

I am currently studying MVVM pattern. I am clear with the concept that Model contains classes, View is just the visual representation of data and ViewModel is the layer that connects Model and View and carry out all manipulations.

But i m still not clear with the fact that from where does Model get data? Since it contains only properties. So even if we use WCF to fill in data, still the question remains:

Is any intermediate class required to fill in data in Model or this work is also carried out via ViewModel?

Model gives data to ViewModel, which in turn gives formatted data to View and View just represents the data. But from where does Model get the data?

推荐答案

模型本身就是数据。更多示例,Model包含描述数据的类,运行时这些类的实例(某些对象图)确实是数据。我认为你应该更准确地区分数据。当你说获取数据,比如人口时,你可能意味着持久形式的这种数据,某些非易失性存储器中某些文件的数据库。这个细节超出了模式所描述的细节层次,并且是模型的一部分,我想说,非常简单。是否分类,取决于您自己的代码设计。在某些复杂情况下,您可能需要在应用程序和数据库之间拥有一个完整的抽象级别,它甚至可能是一个单独的层。或者它可以是对序列化方法的简单调用。 MVVM模式是从这个细节中抽象出来的,这是相对微不足道的。该模式侧重于UI,数据和应用程序状态之间的循环和协作,以及此级别的其他模式,如 MVC MVP



我认为这个架构模式的角色或所有部分在这里得到了清楚的解释: http://en.wikipedia.org/wiki/Model_View_ViewModel [ ^ ]。



您应该明白,这个级别的模式对于计算机科学来说并不是非常基础。它反映了文化,社会和商业环境(尽管有一些自然的历史原因),我们在UI设计和工具方面没有任何认真的统一。 UI往往会更频繁地更改,然后大多数人都会喜欢,并且UI框架在不同平台之间非常不兼容。与UI部件相比,软件的所有其他部分都更加便携。这种情况要求UI与其他应用程序方面密切隔离。我们讨论的所有模式都有助于开发人员将大部分工作与UI隔离开来,使开发更加顺畅,最重要的是,当人们不得不再次更改UI部分时,保留对软件的大量投资。



-SA
"Model" is itself data. More example, Model includes classes which describe the data, instance of those classes during runtime (some object graph) is truly the data. I think you should be more accurate in distinction of data. When you say "get data", and, say, "population", you might mean persistent form of this data, database of some file(s) in some non-volatile storage. This detail goes beyond level of detail described by the pattern and is a part of Model, I would say, pretty marginal. Separate classes or not, depends on your own code design. In some complex situations, you may need to have a whole big abstraction level between your application and database, it could be even a separate tier. Or it could be a simple call to a serializer method. The MVVM pattern is abstracted from this detail, which is relatively trivial. The pattern is focused on like cycle and collaboration between UI, data and application state, as well as other patterns of this level, like MVC and MVP.

I think the roles or all parts of this architectural patterns are explained clearly enough here: http://en.wikipedia.org/wiki/Model_View_ViewModel[^].

You should understand that patterns of this level is not very fundamental to computer science. It rather reflect cultural, social and commercial situation (be some natural historical reasons though), where we don't have any serious unification in the UI design and tools. UI tends to change more often then most people would like and UI frameworks are wildly incompatible between different platforms. All other parts of software are much more portable compared to UI parts. Such situation requires UI to be strongly isolated from other application aspects. All the pattern we discuss help developers to isolate big part of works from UI, make development smoother and, most importantly, to preserve heavy investment in software when people have to change the UI part again.

—SA


简单地说,MVVM不确定模型类从哪里获取数据 - MVVM只是用于开发的模式。



您的模型类可能是以编程方式填充的普通旧对象(例如通过数据库中的数据加载器,或从磁盘加载CSV文件 - MVVM不会'决定这一点。)



您的模型类可以是实体框架类,LLBLGen实体类或DataTable类 - 实际上是任何组合!



我通常会使用某种服务将一个Model对象呈现给ViewModel - 所以Vm知道该服务,并且知道Model类,但是没有关于MOdel如何构造的概念d。



然后服务可以创建模型但是它希望 - 使用上述任何方法 - 并将对象返回到VM。
Put simply, MVVM does not determine from where your model classes get their data - MVVM is just a pattern used for development.

Your model classes could be plain old objects that you populate programatically (say via a datareader from a database, or from loading a CSV file from disk - MVVM doesn't dictate this.)

Your model classes could be Entity Framework classes, LLBLGen Entity classes, or DataTable classes - indeed any combination!

I would generally use some sort of service to present a Model object to the ViewModel - so the Vm knows about the service, and knows about the Model class, but has no concept of how the MOdel was constructed.

The service can then create the Model however it wishes - using any of the methods above - and return the object to the VM.


这篇关于Model从哪里获取MVVM中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆