比较 3-Layer 模式和 MVVM [英] Compare between 3-Layer pattern and MVVM

查看:46
本文介绍了比较 3-Layer 模式和 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道 MVVM.我总是遵循三层模式,一层是 UI,另一层是业务层,最后一层是数据访问层.

I don't know MVVM. I always follow 3-layer patter where one layer is UI, another layer is Business layer and last layer is Data access layer.

在这一层,我们将请求从 UI 发送到业务层,业务层与数据访问层进行交互.在这种模式下,一切顺利,然后我的问题是为什么要学习 MVVM.MVVM 的优势是什么.有什么事情可以用MVVP做很少的功夫.请详细讨论.谢谢.

In this layer we send request from UI to Business layer and Business layer interact with data access layer. In this pattern, everything goes fine then my question why should one learn MVVM. What is the advantage of MVVM. What are the things can be done very little effort using MVVP. Please discuss in detail. Thanks.

推荐答案

图层

与 ppl 在我之前写的相反 - MVVM 模式不是将 UI 层拆分为 3 层,而是将 UI 层拆分为两个附加层 - View 和 ViewModel.

As opposed to what ppl wrote before me - the MVVM pattern is not about splitting the UI Layer into 3 layers, it's about splitting the UI Layer into two additional layers - The View and ViewModel.

所以如果我们有 DAL、BLL 和 UI,现在我们有 Model(DAL & BLL) 和 ViewModel + View(而不是只有一层 UI).

so if we had DAL, BLL, and UI, now we have Model(DAL & BLL) and ViewModel + View (instead of just one layer UI).

它仍然是 3 层,但编排方式不同(如果你真的考虑一下 - DAL 从来都不是真正的层 - 它最多是一个辅助类,所以前面提到的 3 层实际上只是 2 层,现在正在变成MVVM 中的 3 层).

it's still 3 layers but orchestrated differently (and if you really think about it - DAL was never really a layer - it's a helper class at most, so the aforementioned 3-layer was in actuality just 2 layers, that are now becoming 3 layers in MVVM).

原因

如果你仔细想想,你会发现在 3 层架构中,通常 UI 与表示代码和应用程​​序逻辑代码混合在一起.这违反了 SRP(单一职责原则),并且出于多种原因是不好的.在 MVVM 中,UI 层分为两层.ViewModel 负责应用逻辑,而 View 仅负责演示.

if you think about it, you'll see that in 3 layers architecture, usually the UI is mixed with presentation code, and application logic code. this violates SRP (Single Responsibility Principle) and is bad for several reasons. in MVVM the UI Layer is separated into two layers. the ViewModel, which is in charge of the Application Logic, and the View, which is in charge solely on presentations.

这可以让您完成三件非常重要的事情:

This allows you three very important things:

  1. 更好的代码可维护性.

更容易与 VS 设计师和 Blend 合作.又名可混合性.(这可以说是 MVVM 最强大的功能.它确实提高了生产力)

easier to work with VS designer and Blend. aka Blendability. (this is arguably the strongest feature of MVVM. It really boosts up productivity)

允许使用自动化测试来测试 ViewModel,而到目前为止,我们必须测试 UI 本身,并且对 UI 进行自动化测试很复杂.这称为可测试性

allows for testing of the ViewModel with automated tests, whereas up until now we had to test the UI itself, and doing automated tests on UI is complex. This is called Testability

个人笔记;我多年来一直在用 n 层架构写作.一年多前,我开始练习 MVVM.有时这可能会很艰难,但是伙计,这真的值得付出努力.

on a personal note; I've been writing in n-tier architecture for years. I started practising MVVM a little over a year ago. It could be a rough ride in some times, but man, it's really worth the effort.

这篇关于比较 3-Layer 模式和 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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