Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare between 3-Layer pattern and MVVM

Tags:

c#

mvvm

wpf

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.

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.

like image 377
Thomas Avatar asked Feb 28 '11 12:02

Thomas


People also ask

What is difference between MVVM and MVC design pattern?

Key Differences between MVC and MVVMIn MVC, the controller is the entry point to the Application, while in MVVM, the view is the entry point to the Application. MVC Model component can be tested separately from the user, while MVVM is easy for separate unit testing, and code is event-driven.

Is MVVM a 3-tier architecture?

MVVM is arguably a three layer architecture itself. 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.

Which is best MVC or 3-tier architecture?

A 3-tier architecture is linear where the client tier never actually communicates with the data tier--all communication passes through the middle tier. MVC on the other hand is more triangular where the view sends updates to the controller and receives updates from the model and the controller updates the model.

What is MVVM and its advantages?

MVVM facilitates easier parallel development of a UI and the building blocks that power it. MVVM abstracts the View and thus reduces the quantity of business logic (or glue) required in the code behind it. The ViewModel can be easier to unit test than in the case of event-driven code.


3 Answers

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.

so if we had DAL, BLL, and UI, now we have Model(DAL & BLL) and ViewModel + View (instead of just one layer 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).

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. 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. better code Maintainability.

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

  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. This is called Testability

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.

like image 187
Elad Katz Avatar answered Sep 24 '22 03:09

Elad Katz


MVVM is for building the UI layer. It is a pattern that enables a very nice interaction between your business objects and the UI-framework. You don't have to change your 3-Tier pattern. MVVM is on another abstraction level.
Here you find a very good video introducing MVVM and probably answering a lot of questions.

like image 33
HCL Avatar answered Sep 22 '22 03:09

HCL


MVVM is arguably a three layer architecture itself. 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. If you have that sort of layering, then MVVM will not replace it. It will only augment the UI layer, splitting it into its own three layers.

Here's a write-up of MVVM that shows some relation between classic MVC, through MVP and MVVM:

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.

like image 28
Merlyn Morgan-Graham Avatar answered Sep 20 '22 03:09

Merlyn Morgan-Graham