MVVM的VM可以复用吗? [英] Can the VM of MVVM be reused?

查看:61
本文介绍了MVVM的VM可以复用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要简单说明一下:

I just need a simple clarification:

我有一个示例应用程序,其中包含一个球模型和两个视图(假设一个显示球并让您调整大小,如果使用鼠标,单击更改它的颜色,第二个有一个带有两个子控件的控件)(大小文本框和颜色选择器)).

I have an example application with a Model of a ball, and two views (lets say one shows the ball and lets you resize if with the mouse, change it's colour with a click, the second has a control with two child controls (size textbox and colour picker)).

MVVM 是否说我必须在这里有两个 VM,每个特定视图一个,或者我可以在不破坏模式的情况下重用 VM?

Does MVVM say I must have two VM here, one for each specific View, or am I allowed to reuse the VM without breaking the pattern?

作为一个后续问题,如果我需要有两个 VM 是否合法,根据模式将一个作为另一个的派生类型,或两者都作为基类的派生类型,或基于复合类在公共部分?基本满足了两种类型的需求,但提高了复用性?

As a follow up question, if I am required to have two VM's is it legitimate, according to the pattern to have one as a derived type of the other, or both as derived types of a base class, or composite classes based on common parts? Basically fulfilling the need for two types but improving reuse?

我的例子是人为的,我试图说明两个视图都会影响模型的两个相同的属性.两个视图之间的最大尺寸、可用颜色等是相同的.

My example is contrived, I've tried to make the point that both Views affect the same two properties of the model. Max size, available colours, etc are the same between both views.

谢谢

推荐答案

在 MVVM 中使用您在其他任何地方使用的相同编码实践.尤其要保持干燥.因此,如果您可以使用相同的视图模型,那么就这样做.我会说重用视图模型是非常罕见的.如果您以后必须重构为两个单独的视图模型,但可以从公共基类派生以重用公共部分等,那么请这样做.

Use the same coding practices in MVVM that you use anywhere else. In particular, stay DRY. So if you can use the same view model, then do so. I would say reuse of a view model is extremely rare though. If you later have to refactor into two separate view models but can derive from a common base class to reuse common parts, etc. then do so.

这篇关于MVVM的VM可以复用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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