MVVM模式是否损坏? [英] Is MVVM pattern broken?

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

问题描述

我有一个基于MVVM模式的WPF应用程序.一切正常.在视图模型的业务逻辑中,我需要调用一些对话框.我已经通过接口实现了此接口(接口实现在View层中).
对于对话框,我需要设置父窗口,因此接口中的函数具有参数"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中,对于父窗口,我具有对象"类型的属性视图".我将此属性作为实例化对话框的参数.

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.

由于视图"属性来自对象"类型,并且是从视图"层进行设置并转发回视图"层,因此视图模型"层不依赖于视图"层.

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天全站免登陆