WPF/MVVM - 将 ViewModels 保留在应用程序组件中还是分开? [英] WPF/MVVM - keep ViewModels in application component or separate?

查看:32
本文介绍了WPF/MVVM - 将 ViewModels 保留在应用程序组件中还是分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序,我在其中使用了 MVVM 模式.

I have a WPF application where I'm using the MVVM pattern.

我为需要用户输入的操作激活了 VM,因此需要从 VM 激活视图.

I get the VM activated for actions that require user input and thus need to activate views from the VM.

我开始将 VM 分成单独的组件/程序集,部分原因是我将它们视为单元可测试部分,部分原因是视图应该依赖于 VM,而不是相反.但是当我需要打开一个窗口时,VM 不知道该窗口.

I've started out separating the VMs into a separate component/assembly, partly because I see them as the unit testable part, partly because views should rely on VM, not the other way round. But when I then need to bring up a window, the window is not known by the VM.

我发现的所有介绍都将 VM 放在 WPF/App 组件中,从而消除了问题.

All introductions I find place the VM in the WPF/App component, thus eliminating the problem.

本文建议将它们放在单独的层中:http://waf.codeplex.com/wikipage?title=Architecture%20-%20Get%20The%20Big%20Picture&referringTitle=Home

This article recommends keeping them in separate layers : http://waf.codeplex.com/wikipage?title=Architecture%20-%20Get%20The%20Big%20Picture&referringTitle=Home

据我所知,我有以下选择

As I see it, I have the following choices

  1. 将虚拟机移动到 WPF/App 程序集以允许虚拟机直接访问窗口.

  1. Move VMs to the WPF/App assembly to allow VMs to access the windows directly.

将视图的接口放在VM-assembly中,在WPF/App assembly中实现视图,并通过IOC或其他方式注册连接.

Place interfaces of views in VM-assembly, implement views in WPF/App assembly and register the connection through IOC or alternative ways.

将来自 VM 的请求"提交到路由请求的某种机制/总线(但哪种机制!?例如 Prism 中的某些东西?!)

File a 'request' from the VM into some mechanism/bus that routes the request (but which mechanism!? E.g something in Prism?!)

有什么建议?

感谢您的任何评论,

丹麦安德斯

推荐答案

不要选择选项 1.您将添加一个不需要的从 VM 到 V 的依赖项.

Don't pick option 1. You'll be adding an unwanted dependency from VM to V.

选项 2 和 3 都有效并且正在使用.在这些之间进行选择有时是一个品味问题.我认为 IOC 启用/允许模拟更好,而消息总线适用于小型应用程序.

Options 2 and 3 are both valid and being used. Picking between these is sometimes a matter of taste. I think that IOC enables/allows mocking better whereas a messagebus works fine for small applications.

这篇关于WPF/MVVM - 将 ViewModels 保留在应用程序组件中还是分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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