MVVM:如何处理嵌套 ViewModel 之间的交互? [英] MVVM: How to handle interaction between nested ViewModels?

查看:59
本文介绍了MVVM:如何处理嵌套 ViewModel 之间的交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验经常提到的 MVVM 模式,但在某些情况下我很难定义清晰的边界.在我的应用程序中,我有一个对话框,允许我创建到控制器的连接.对话框有一个 ViewModel 类,很简单.但是,该对话框还承载了一个附加控件(由 ContentTemplateSelector 选择),该控件因所连接的控制器的特定类型而异.这个控件有自己的 ViewModel.

I'm been experimenting with the oft-mentioned MVVM pattern and I've been having a hard time defining clear boundaries in some cases. In my application, I have a dialog that allows me to create a Connection to a Controller. There is a ViewModel class for the dialog, which is simple enough. However, the dialog also hosts an additional control (chosen by a ContentTemplateSelector), which varies depending on the particular type of Controller that's being connected. This control has its own ViewModel.

我遇到的问题是,当我按 OK 关闭对话框时,我需要实际创建请求的连接,这需要在特定于控制器的内部 ViewModel 类中捕获的信息.简单地让所有特定于控制器的 ViewModel 类实现一个构建连接的通用接口是很诱人的,但是内部 ViewModel 真的应该负责这个构建吗?

The issue I'm encountering is that, when I close the dialog by pressing OK, I need to actually create the requested connection, which requires information captured in the inner Controller-specific ViewModel class. It's tempting to simply have all of the Controller-specific ViewModel classes implement a common interface that constructs the connection, but should the inner ViewModel really be in charge of this construction?

我的一般问题是:对于 ViewModel 应该如何相互交互,是否有任何普遍接受的设计模式,特别是当父"VM 需要子"VM 的帮助以便知道该怎么做时?

My general question is: are there are any generally-accepted design patterns for how ViewModels should interact with eachother, particularly when a 'parent' VM needs help from a 'child' VM in order to know what to do?

我确实想出了一个比我最初想象的更简洁的设计,但我仍然不确定这是否是实现此目的的正确"方式.我有一些后端服务,它们允许 ContentTemplateSelector 查看控制器实例并伪魔法地找到要为连接构建器显示的控件.困扰我的是我的顶级 ViewModel 必须查看生成控件的 DataContext 并将其转换为适当的界面,这似乎是一个坏主意(为什么视图的DataContext 与创建连接有什么关系?)

I did come up with a design that's a bit cleaner than I was originally thinking, but I'm still not sure if it's the 'right' way to do this. I have some back-end services that allow a ContentTemplateSelector to look at a Controller instance and pseudo-magically find a control to display for the connection builder. What was bugging me about this is that my top-level ViewModel would have to look at the DataContext for the generated control and cast it to an appropriate interface, which seems like a bad idea (why should the View's DataContext have anything to do with creating the connection?)

我最终得到了这样的东西(简化):

I wound up with something like this (simplifying):

public interface IController
{
    IControllerConnectionBuilder CreateConnectionBuilder();
}

public interface IControllerConnectionBuilder
{
    ControllerConnection BuildConnection();
}

我有我的内部 ViewModel 类实现 IControllerConnectionBuilder 并且控制器返回内部 ViewModel.然后顶层 ViewModel 将这个 IControllerConnectionBuilder 可视化(通过伪魔法机制).它仍然让我有点困扰,它是我的内部 ViewModel 执行构建,但至少现在我的顶级 ViewModel 不必知道肮脏的细节(它甚至不知道或不关心可视化控件正在使用视图模型).

I have my inner ViewModel class implement IControllerConnectionBuilder and the Controller returns the inner ViewModel. The top-level ViewModel then visualizes this IControllerConnectionBuilder (via the pseudo-magical mechanism). It still bothers me a little that it's my inner ViewModel performing the building, but at least now my top-level ViewModel doesn't have to know about the dirty details (it doesn't even know or care that the visualized control is using a ViewModel).

如果有办法进一步清理它,我欢迎其他想法.我仍然不清楚 ViewModel 有多少可以"的责任.

I welcome additional thoughts if there are ways to clean this up further. It's still not clear to me how much responsibility it's 'okay' for the ViewModel to have.

推荐答案

一个适用于视图模型之间交互的选项是直接绑定到 观察者 类位于视图模型类之间.

An option which works well for interaction between viewmodels is to bind directly to observer classes sitting between the viewmodel classes.

这篇关于MVVM:如何处理嵌套 ViewModel 之间的交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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