为什么我应该在 Silverlight 应用程序中使用 MVVM? [英] Why should I use MVVM in Silverlight app?

查看:39
本文介绍了为什么我应该在 Silverlight 应用程序中使用 MVVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我们应该使用 MVVM 来实现 Silverlight 应用程序.它有什么优势?

I want to know why we should use MVVM to implement Silverlight app. What're it's advantages?

我们不对 ViewModel 进行单元测试,所以我想知道其他原因.

We don't do Unit Test for ViewModel, so I want other reasons.

以下是我对人们常说的一些优势的问题:

Below are my questions about some advantages people usually say:

1.松散耦合:当我们使用 MVVM 时,视图依赖于 ViewModel 而不是视图,为什么它是松耦合的?

1.Loosely Coupled : When we use MVVM , a view rely on ViewModel but not a view, why it's loosely coupled?

2.如果我在代码隐藏中提供公共方法,它们也可以提供可重用性.

2.If I provide public methods in a code-behind, they can also provide reusability.

推荐答案

我们不对 ViewModel 进行单元测试,

We don't do Unit Test for ViewModel,

使用 MVVM,不仅仅是对 ViewModel 进行单元测试.理想情况下,您的 VM 应该非常精简,并且只有视图需要的属性.因此,实际上没有必要测试 VM.

With MVVM, it isn't just about unit testing ViewModel. Ideally, your VM should be very thin and only have properties needed by view. So, it isn't really necessary to test the VM.

但是,如果没有虚拟机,您如何跨层进行特性/功能测试?在 Silverlight 中,为了便于测试,您应该使用命令,而不是在代码隐藏文件中编写代码.这允许您在单元测试时模拟按钮单击和其他 GUI 事件.使用 MVVM 模式和命令,您可以测试所有 C# 代码(不是 xaml),直到 UI(转换器、VM 等).

But, without a VM, how do you do your your feature/functional testing across layers? In Silverlight, to facilitate testing you should use commands, instead of writing code in code-behind files. This allows you to simulate button click and other GUI events while unit testing. Using MVVM pattern along with commands, you can test all of C# code (not xaml), right up to UI (Converter, VMs, etc).

如果我在一个代码隐藏,他们还​​可以提供可重用性.

If I provide public methods in a code-behind, they can also provide reusability.

没有详细说明这是一个糟糕的设计,我想问你,这如何提供可重用性?如果创建用户控件,那么代码隐藏类是控件吗?您想创建控件的实例并使用它们吗?这就像在说为什么我们需要成员方法,我们可以创建公共静态方法并访问它们.我有一个强烈的意见,如果我们不想使用 WPF/Silverlight 提供的自动绑定,那么最好不要使用这些技术.要充分利用绑定的全部功能,MVVM 必不可少.

Not going into details of how that is a bad design, I want to ask you, How does that provide reusablity? If you create a user control, then the code-behind class is a control? You want to create instances of your control and use them? This is like saying that why do we need member methods, we can just create public static methods and access them. I have a strong opinion that if we don't want to use the automatic binding provided by WPF/Silverlight, then it is better NOT to use these technologies. And to exploit the full capabilities of binding, MVVM is essential.

为什么它是松耦合的?

VM 是您观点的重要组成部分.它没有与视图分离.正如我所说,您的 VM 应该尽可能精简,只有您的视图需要的公共属性.您的业​​务逻辑将与您的视图(和虚拟机)分离.

VM is very much part of your view. It is not decoupled from the view. As I have said, your VM should be as thin as possible with only public properties needed by your view. Your business logic will be decoupled from your view (and VM).

这篇关于为什么我应该在 Silverlight 应用程序中使用 MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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