将常见的 ViewModel 功能推送到基类中? [英] Push common ViewModel functionality into a base class?

查看:25
本文介绍了将常见的 ViewModel 功能推送到基类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MVVM 与 Prism 和 Silverlight 一起使用.我对一个模型有多种不同的看法.当我编写更多视图时,他们的 ViewModel 似乎复制了许多与处理这个模型相关的常见代码.我不想在所有 VM 中重复相同的公共代码,而是想将其推回模型中(这可能会混淆太多关注点).或者可能进入一些常见的 ViewModel 基类?或者也许我的虚拟机需要在它们和模型之间使用第二级共享虚拟机"?这个单一的共享实例,即 2nd-level-VM 将整合多个常规 VM 共享的行为和状态.

I'm using MVVM with Prism and Silverlight. I have multiple different views of one model. As I am writing more views their ViewModels seem to duplicate a lot of common code related to handling this one model. Rather than repeating the same common code in all the VMs I am tempted to push it back into the model (which would probably mix concerns too much). Or maybe into some common ViewModel base class? Or perhaps my VMs need a 2nd level of "shared VM" between them and the model? This single shared instance, 2nd-level-VM would consolidate the behavior and state shared by the multiple regular VMs.

对这些问题和可能的方法有何评论?

Any comments about these issues and possible approaches?

感谢大家的评论.我可能应该告诉您更多有关特定共享"VM 代码的信息.

Thanks for the comments guys. I probably should have told you more about the specific "shared" VM code in question.

我可以看到将一些未来代码放在 VM 基类中,但是我正在查看的特定共享"代码似乎属于由实现的 INotifyPropertyChanged模型本身.这部分基于此其他线程.

I can see putting some future code in a VM base class, but the particular "shared" code I'm looking at seems to belong in an INotifyPropertyChanged implemented by the model itself. This is partly based on this other thread.

我不认为这违反了 SoC,因为该模型本质上是动态的.它的某些属性仅在特定时间有效.模型的动态特性不仅对 UI 很重要,适当的单元测试也会关心它.因此这个模型似乎需要一个 INotifyPropertyChanged.

I don't think this violates SoC, because the model is inherently dynamic. Some of its properties are only valid at certain times. That dynamic nature of the model is not just something that's important for the UI, a proper unit-test would also care about it. Hence this model seems to need an INotifyPropertyChanged.

对此有何评论?

推荐答案

如果公共代码可以被所有 ViewModel 共享,那么将其放入基本 ViewModel 类型是值得的.

If the common code can be shared by all ViewModels, then it's worth putting it into a base ViewModel type.

如果公共代码仅由与特定模型交互的 ViewModel 共享,那么共享"ViewModel 是可行的方法.

If the common code is only shared by ViewModels that interact with a particular Model, then a "shared" ViewModel is the way to go.

这篇关于将常见的 ViewModel 功能推送到基类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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