将模型与视图模型分离的意义何在? (MVVM) [英] What's the point of separating the Model from the View Model? (MVVM)

查看:196
本文介绍了将模型与视图模型分离的意义何在? (MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我不正确理解MVVM模式,因为拥有Model和ViewModel类对我来说似乎是多余的.

I don't think I understand the MVVM pattern properly because having a Model and ViewModel class seems redundant to me.

我对模型的理解是基本上添加一个类的次要细节,并让ViewModel处理所有逻辑和实现.如果是这样,为什么要将两者分开?您不能在视图模型中创建变量,属性等,并且仍然在其中包含逻辑吗?

My understanding of the Model is to basically add in minor details of a class and let the ViewModel handle all the logic and implementation. If that is the case why separate the two? Couldn't you create the variables, properties and such in the view model and still have the logic in there?

对我来说,某种程度上听起来像C ++.您具有描述类的头文件和定义类的实现文件.在c#中以这种方式执行此操作有什么意义吗?

To me it sounds like C++ in a way. You have the header file which describes the class and the implementation file which defines the class. Is there any point to doing it this way in c#?

我觉得我不了解分离,因为我不完全了解MVVM模式.如果有人可以为我澄清这一点,那就太好了.

I feel like I don't understand the separation because I don't fully understand the MVVM pattern. If someone can clarify it for me that would be awesome.

谢谢.

推荐答案

要尝试使之成为更切实的答案,让我们来看一个示例.您想在一个漂亮的WPF程序中使用相同的旧计算器"示例.

To try and make this a more tangible answer , lets look at an example. You want to have the same old "calculator" example in a nice shinny WPF program.

您应该记住,您实际上是在很久以前就为另一个项目编写的,而没有跳入视图模型中并在视图模型中编写所有内容,并且您实际上已经足够聪明,可以将所有计算器功能都包含在单独的(可重用)dll中.
因此,您有了模型.

Instead of jumping in and writing everything in the view model, you remember that you actually wrote this ages ago for a different project, and you actually were smart enough to have all the calculator functionality in a separate (and reusable) dll.
So, you got your model.

现在剩下的就是您的GUI.您在WPF(视图)中绘制了一个漂亮的闪亮窗口,然后需要将调用和数据从dll桥接到视图.您猜对了……这是您的ViewModel:).

Now all that's left is your GUI. You draw a nice shiny window in WPF (View) , and then you need to bridge your calls and data from the dll to the view. You guessed right ... this is your ViewModel :).

另一方面,我们的想法是能够在一个大团队中工作,其中有些人负责逻辑(模型),有些设计师负责视图,而其他人(当然可以是以上任何一种)可以让这些位与视图模型一起工作.

On another note, the idea is to be able to work on a big team where some people work on the logic (model), some designers work on the view, and someone else (can be any of the above ofcourse) can get the bits working together with the view models.

这篇关于将模型与视图模型分离的意义何在? (MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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