.net 从 MVC 到 MVVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3731041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
From MVC to MVVM
提问by srmark
I don't want to store my domain model classes in the same assembly as my web platform. The Models folder in the project structure is therefore useless to me. I've however just finished the Music Store Tutorial and noticed how they create a "ViewModels" folder which makes lots of sense to me.
我不想将域模型类存储在与 Web 平台相同的程序集中。因此,项目结构中的 Models 文件夹对我来说毫无用处。然而,我刚刚完成了音乐商店教程,并注意到他们如何创建一个“ViewModels”文件夹,这对我来说很有意义。
Does it make sense to just treat the Models folder as a ViewModels folder? Do many people do this? Is there such a pattern as MVVM?
将 Models 文件夹视为 ViewModels 文件夹是否有意义?很多人都这样做吗?有没有像 MVVM 这样的模式?
回答by GSerg
A view model is something to add another level of abstraction in case you don't completely trust your presentation code (or simply find this kind of encapsulation more elegant).
如果您不完全信任您的表示代码(或者只是发现这种封装更优雅),则视图模型可以添加另一个抽象级别。
That is, if your Personclass has a Deletemethod or a SSNumberproperty, you might want to not pass this object to a view, as this, conceptually, enables it to call Delete or display the SSN, which it must not be able to.
To avoid this situation, you create another class, PersonViewModel, that only contains information/methods that are safe to be called from a view.
也就是说,如果您的Person类具有Delete方法或SSNumber属性,您可能不希望将此对象传递给视图,因为从概念上讲,这使其能够调用 Delete 或显示 SSN,而这是它必须不能做到的。
为了避免这种情况,您创建了另一个类,PersonViewModel,它只包含可以从视图中安全调用的信息/方法。
This has little to do with taking the model logic out of an MVC application. You can create a separate project for your model and reference it from your web application regardless of whether you use ViewModel encapsulation. Doing so is encouraged by books I've read so far.
这与从 MVC 应用程序中取出模型逻辑关系不大。无论您是否使用 ViewModel 封装,您都可以为您的模型创建一个单独的项目并从您的 Web 应用程序中引用它。到目前为止,我读过的书都鼓励这样做。
回答by Dx_
I think you refer to the MVVM pattern (Model-View-ViewModel)
我认为您指的是 MVVM 模式(Model-View-ViewModel)
There is not such a thing like MVVC.
没有像 MVVC 这样的东西。
回答by Mohamed Abed
The Domain Model is designed in terms of business logic and business abstraction, it is targeted to solve business problem maybe utilizing object oriented techniques, the result is a domain model with entities and value objects references each others and interact with others to achieve the business logic goals.
域模型是从业务逻辑和业务抽象的角度设计的,它旨在解决业务问题,可能利用面向对象的技术,结果是一个实体和值对象相互引用并相互交互以实现业务逻辑的域模型目标。
Presentation on the other hand is a different perspective, you mostly needs to flatten the domain objects to make it easier to bind, you also may not be interested of some attributes and properties of the domain model entities in the presentation layer, thus the presentation model (View Model) is a more customization of the model for view purposes, the structure could be different, you may remove some fields that are unneeded for presenataion and also add some fields just for presentation purposes (like "IsIdEnabled" or SliderWidth, ...)
另一方面,表示是一个不同的角度,您主要需要将领域对象扁平化以使其更容易绑定,您也可能对表示层中域模型实体的某些属性和属性不感兴趣,因此表示模型(视图模型)是用于视图目的的模型的更多自定义,结构可能会有所不同,您可以删除一些不需要的字段,也可以添加一些仅用于演示目的的字段(例如“IsIdEnabled”或 SliderWidth,.. .)
回答by jjpcondor
As Dx_ stated above, there is not such a thing like MVVC.
正如上面的 Dx_ 所说,没有像 MVVC 这样的东西。
But, this fine talk describes MVVM very well: Deep Dive MVVM
但是,这个精彩的演讲很好地描述了 MVVM:Deep Dive MVVM

