从 MVC 到 MVVM
我不想将域模型类存储在与 Web 平台相同的程序集中。因此,项目结构中的 Models 文件夹对我来说毫无用处。然而,我刚刚完成音乐商店教程,并注意到他们如何创建一个“ViewModels”文件夹,这对我来说很有意义。
将 Models 文件夹视为 ViewModels 文件夹是否有意义?很多人都这么做吗?有MVVM这样的模式吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
视图模型是添加另一个抽象级别的东西,以防您不完全信任您的表示代码(或者只是发现这种封装更优雅)。
也就是说,如果您的
Person
类具有Delete
方法或SSNumber
属性,您可能不希望将此对象传递给视图,如下所示从概念上讲,这使其能够调用删除或显示 SSN,但它不能这样做。为了避免这种情况,您创建另一个类
PersonViewModel
,它仅包含可以从视图安全调用的信息/方法。这与从 MVC 应用程序中取出模型逻辑没有什么关系。无论您是否使用 ViewModel 封装,您都可以为您的模型创建一个单独的项目并从您的 Web 应用程序引用它。到目前为止我读过的书鼓励这样做。
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
Person
class has aDelete
method or aSSNumber
property, 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.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.
我认为你指的是MVVM模式(Model-View-ViewModel)
没有这样的事情像MVVC。
I think you refer to the MVVM pattern (Model-View-ViewModel)
There is not such a thing like MVVC.
领域模型是根据业务逻辑和业务抽象来设计的,它的目标是解决业务问题,可能利用面向对象的技术,结果是实体和值对象相互引用并交互以实现业务逻辑的领域模型目标。
另一方面,表示是一个不同的视角,您大多需要扁平化领域对象以使其更容易绑定,您也可能对表示层中的领域模型实体的某些属性和属性不感兴趣,因此表示模型(视图模型)是用于视图目的的模型的更多自定义,结构可能不同,您可以删除一些演示不需要的字段,并添加一些仅用于演示目的的字段(例如“IsIdEnabled”或 SliderWidth,.. .)
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, ...)
正如Dx_上面所说,不存在像MVVC这样的东西。
但是,这篇精彩的演讲很好地描述了 MVVM:深入了解 MVVM
As Dx_ stated above, there is not such a thing like MVVC.
But, this fine talk describes MVVM very well: Deep Dive MVVM