简体   繁体   English

比较 3-Layer 模式和 MVVM

[英]Compare between 3-Layer pattern and MVVM

I don't know MVVM.我不知道 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,另一层是业务层,最后一层是数据访问层。

In this layer we send request from UI to Business layer and Business layer interact with data access layer.在这一层,我们从 UI 向业务层发送请求,业务层与数据访问层进行交互。 In this pattern, everything goes fine then my question why should one learn MVVM.在这种模式下,一切顺利,然后我的问题是为什么要学习 MVVM。 What is the advantage of MVVM. MVVM 的优势是什么。 What are the things can be done very little effort using MVVP.有什么事情可以用MVVP做很少的功夫。 Please discuss in detail.请详细讨论。 Thanks.谢谢。

The Layers

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

so if we had DAL, BLL, and UI, now we have Model(DAL & BLL) and ViewModel + View (instead of just one layer UI).所以如果我们有 DAL、BLL 和 UI,现在我们有 Model(DAL & BLL) 和 ViewModel + View(而不是只有一层 UI)。

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 层,但编排方式不同(如果你真的考虑一下 - DAL 从来都不是真正的层 - 它最多是一个辅助类,所以前面提到的 3 层实际上只是 2 层,现在变成了 3 层MVVM)。

The Reasons原因

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.如果你仔细想想,你会发现在 3 层架构中,通常 UI 与表示代码和应用程​​序逻辑代码混合在一起。 this violates SRP (Single Responsibility Principle) and is bad for several reasons.这违反了 SRP(单一职责原则),并且出于多种原因是不好的。 in MVVM the UI Layer is separated into two layers.在 MVVM 中,UI 层分为两层。 the ViewModel, which is in charge of the Application Logic, and the View, which is in charge solely on presentations. ViewModel 负责应用逻辑,而 View 仅负责演示。

This allows you three very important things:这可以让你做三件非常重要的事情:

  1. better code Maintainability .更好的代码可维护性

  2. easier to work with VS designer and Blend.更容易与 VS 设计师和 Blend 一起工作。 aka Blendability .又名可混合性 (this is arguably the strongest feature of MVVM. It really boosts up productivity) (这可以说是 MVVM 最强大的功能。它确实提高了生产力)

  3. 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.允许使用自动化测试来测试 ViewModel,而到目前为止,我们必须测试 UI 本身,并且对 UI 进行自动化测试很复杂。 This is called Testability这称为可测试性

on a personal note;在个人笔记上; I've been writing in n-tier architecture for years.我多年来一直在用 n 层架构写作。 I started practising MVVM a little over a year ago.一年多前,我开始练习 MVVM。 It could be a rough ride in some times, but man, it's really worth the effort.有时这可能会很艰难,但是伙计,这确实值得付出努力。

MVVM is for building the UI layer. MVVM 用于构建 UI 层。 It is a pattern that enables a very nice interaction between your business objects and the UI-framework.它是一种模式,可以在您的业务对象和 UI 框架之间实现非常好的交互。 You don't have to change your 3-Tier pattern.您不必更改您的 3 层模式。 MVVM is on another abstraction level. MVVM 处于另一个抽象级别。
Here you find a very good video introducing MVVM and probably answering a lot of questions. 在这里,您可以找到一个非常好的介绍 MVVM 的视频,并且可能回答了很多问题。

MVVM is arguably a three layer architecture itself. MVVM 本身可以说是一个三层架构。 Those layers all exist within the same application.这些层都存在于同一个应用程序中。

"3-layer" also sometimes refers to n-tier architecture , which is more about separating the UI, the service layer, and the data layer, onto separate servers. “三层”有时也指n 层架构,它更多地是将 UI、服务层和数据层分离到单独的服务器上。 If you have that sort of layering, then MVVM will not replace it.如果你有那种分层,那么 MVVM 不会取代它。 It will only augment the UI layer, splitting it into its own three layers.它只会增加 UI 层,将其拆分为自己的三层。

Here's a write-up of MVVM that shows some relation between classic MVC, through MVP and MVVM:这是 MVVM 的一篇文章,通过 MVP 和 MVVM 展示了经典 MVC 之间的一些关系:

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Also see my answer to this other question .另请参阅我对另一个问题的回答 It explains some of the reason you would use MVVM over older variations on MVC .它解释了您会使用 MVVM 而不是MVC 的旧变体的一些原因。

MVVM 与 WPF、Silverlight/Moonlight 和 Windows Phone 7 尤其相关,因为它利用了这些框架中内置的强大数据绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM