MVVM 模式被打破了吗? [英] Is MVVM pattern broken?

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

问题描述

我有一个基于 MVVM 模式的 WPF 应用程序.一切正常.在 View-Model 的业务逻辑中,我需要调用一些对话框.我已经通过一个接口实现了它(接口实现在一个视图层).
对于对话框我需要设置父窗口,所以接口中的函数有一个参数parentView".

I have a WPF-Application based on MVVM-Pattern. All works fine. In the business logic of View-Model I need to invoke some dialogs. I have implemented it through an interface(interface implementation is in a View layer).
For the dialogs I need to set the parent window, so the function in interface has an argument "parentView".

在我的 ViewModel 中,我有一个来自父窗口类型object"的属性View".我将此属性作为对话框实例化的参数.

In my ViewModel I have a property "View" from type "object" for a parent window. This property I put as argument for instantiating of a dialog.

由于View"-property 来自object"类型,并且是从 View 层设置并转发回 View 层,因此 View-Model 层对 View 层没有依赖关系.

Since "View"-property is from type "object" and is set from View layer and forwarded back to the View layer, the View-Model layer has no dependencies on View layer.

你怎么看,我是否打破了 MVVM 模式?

What do you think, have I broken the MVVM-Pattern?

推荐答案

你怎么看,我是否打破了 MVVM 模式?

What do you think, have I broken the MVVM-Pattern?

没有.视图模型不依赖于视图,它只知道一个可以在单元测试中轻松模拟的接口.因此,只要视图"只是某种事物的抽象,这并不会真正打破模式.

No. The view model have no dependency upon the view, it only knows about an interface that you could easily mock in your unit tests. So this doesn't really break the pattern as long as "View" is just an abstraction of something.

出于类型安全的原因,您可能应该考虑将参数的类型从 object 更改为强类型接口类型.

For type-safety reasons you should probably consider changing the type of the parameter from object to a strongly-typed interface type though.

这篇关于MVVM 模式被打破了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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