Thursday, 8 May 2014

3-Tire, MVC , MVP and MVVM architectures

It’s a bit confusion to each one and they just skip the importance of it.
First let me talk about the 3-tier (n-tier) architecture and what are the problems it has and why the new patterns came as it was the most popular architecture in their time. Also even now most of the companies are preferring 3 tier architecture for their development of the projects.

3-Tier Architecture: 
One of the most important and useful architecture to create the web applications. It has mainly 3 layers-
- UI or 
Presentation layer (.aspx, .ascx, aspx.cs, .ascx.cs files)
- 
Business Logic Layer (.cs classes)
- Data Access Layer (.cs classes)
These are the 3 main layers which are used while developing the applications in 3-tier architecture.

The 
UI Layer or Presentation Layer contains the main User Interface which will be viewable to the users. So all the screens, User defined controls, custom controls etc comes under UI or presentation layer.

Business Logic Layer is the middle layer which is used for the development of business logics for the application so that the validation should be performed or business requirements should be fulfilled before sending data to the Data Access layer. Most of the validations are performed at this level. This layer contains the business objects and the methods used for the communication between the UI and Data Access Layer.
Here few architects think of separating this layer with Business Object layer to make the design mode readable and easy. So they create one more layer called Business Objects layer which contains the public properties which can be used throughout the application.

Data Access Layer is used for all the database operations- Insert, Update, Delete, Select etc. So all the logic which is used for these operations are performed at this layer. Calling stored procedures, passing parameters etc.

So 3-Tier architecture uses these layers for the communications and gets the result to the UI layer. The communication is done based on the request from the user by UI layer. This request goes through the business logic layer and then if any validations need to perform. It will validate and if the validation passed, it sends these requests to the Data Access Layer. The data access layer performs the database operations and then the results is send back to
business layer and then to the UI.
Hope till now it
’s all going good...

Now the problem comes- 
1. If we add one more method in the business logic layer or in data access layer or in UI, we need to update all the layers. It means we need to implement that method in all the layer. Similarly if we remove one method from any of these layers, it will give you the error.
2. If we don
’t want to perform the validations, then also it will go to business logic layer and then to the database layer. Because here all the layers are tightly bounded to each other and are dependent to each other.
3. Always the floe will be UI <---> Business Logic Layer <---> Data Access Layer
There is no skip at any place.

Why MVC? 
MVC is also the components of triad means similar to the 3-tier architecture. It also has 3 layers-
- View (UI in 3-tier architecture) (.aspx, .ascx files)
- Controller (Business Logic Layer in 3-tier architecture) (.cs files)
- Model (Data Access layer in 3-tier architecture) (.cs files)
Here you can see the main difference between the UI and View is that the View does not contain .cs file attached with it. It contains only .aspx, .ascx files.
View- It is similar to the .aspx pages but with the .cs files. These are again used for the user interface.
Controller- It is slight difference than the Business Logic layer classes. It contains all the events, methods and properties related to the corresponding view.
Model- It is similar to the Data Access Layer classes where the data access logic is written. So there is no difference between the Model and Data Access Layer classes. It
’s the programmer’s view that for the database operations what they use to connect to the database and do the operations.
How MVC works differently than 3-Tier architecture -
In MVC, the processing always starts from the Controller unlike from UI as compare to 3-Tier architecture. For example, the user has made the request to get the product details by supplying the product id and the URL is something like this
http://www.MyWebSite.com/ProductController/GetProductDetails?id=2 
Here you can see the name ProductController. This is the controller class which will handle the request to process and responsible for getting the product details for the product id as 2.
If there is a controller class, there must be the view for the same controller class. Like in this URL, the corresponding Product view will be in the View section for the ProductController class. Then only the application will recognize that which view should be populated here. 
In this example, first the URL will be checked for the controller class i.e. ProductController and then it will check for the next method which is used i.e.GetProductDetails and then parameter if any like in this case, id=2. It will then call the corresponding model class to get the product details for the id as 2. After retrieving the details, it will search for the Product view and populate the product view.
Now the task is done...
How it has benefit than old architecture-
1. No need to go from View <---> Controller <---> Model like in 3-tier architecture. So these layer are not tightly bounded to each other.
2. If we add any new method to the controller class or model class or add any new design to the view, it will not impact any other layer. Because if we add new method in the controller and didn
’t call it, it will not execute as it is not dependent to any other layer.
3. It's not necessary that the Controller will always call the Model class, if there is no need to do any database operations, then just call the view to open the blank screen.
So these are the main advantages of MVC architecture. Due to these advantages, it
’s faster in execution than the 3-Tier architecture.

Why MVP if MVC is fulfilling all the requirements? 
Yes, why MVP if the MVC was good enough.
No, MVC was not good enough because there were few issues with MVC:
1. The execution starts from Controller i.e. Business Logic which is violating the rules of W3C. All the web application should start from the user Interface not from the business logic. As business logic can be exposed if proper security is not implemented.
2. Model and UI are still connected as they are attached by a logical layer.
MVP mainly came for automated unit testing. It is a derivative of the MVC where the Controller of MVC replaced by the Presenter.
When the user triggers/calls a method of view, a method of presenter without parameter (default constructor) gets invoked. And the view is displayed.
Here the execution starts always from the View so the issue 1 of MVC gets resolved.
It mainly made to test the application without View. Only by using the presenter we can test the whole application. Presenter mainly acts upon model and updates the view accordingly. So there is no coupling between the View and Model here. Whenever the Model change, automatically the view gets updated.
So all the issues of MVC gets resolved in MVP pattern.
Now if MVP was fulfilling all the requirements and no coupling between the View and Model then why MVVM??
Why MVVM if MVP was fulfilling all the requirements and solving all the issues by 3-Tier architecture and MVC? 
Yes, MVVM is also called as Specialized MVC where the Controller is replaced by using the View Model.
Here the View is just below the UI layer. View Model exposes the data and command objects according to the need of View. We can say that View Model is the blue print of View which contains all the properties, methods, events which the View needs. View Model pulls the data from the Model and then accordingly the View gets updated. This works well because this model created by making the customer as the key value and keeping the user experience as the first priority. It uses the observer pattern to make the changes as soon as the View Model is updated so the user will not get any flicking of the current page. It
’s looking like working with windows application.