Tuesday, October 25, 2016

Model View Controller Architecture and ASP.NET

Maintainability is the most important aspect/parameter of software design, when we design our code to be maintainable then other things like scalability,performance and security can be easily achieved.

Separation of Concerns :

“Concerns” – This means a feature which is mapped to a “module” in software.  SOC is breaking the system into distinct , independent modules.

Modularity -  SOC is achieved through  modular code. This modularity in code can be achieved through information hiding.  Information hiding is nothing but exposing the standard interfaces and hiding the internal implementation details

Cohesion:

Cohesion is nothing but keeping a class focused on what it should do. Only related methods should be put together in a class. This reduces dependency. Putting unrelated methods in a class reduces focus of the class.

Coupling:

Coupling is nothing but dependency between one module and another. If the modules are tightly coupled then change in one module will affect another module. So any design should be loosely coupled.

The above mentioned factors are addressed in the Model View Controller Architecture.

Model View Controller:

The main idea behind the model view controller pattern is to separate the UI Elements from the Model. Separate Presentation from the Model - which is essentially an "Software" Description of a real world system.

Model - In software we often "Model" the real world scenarios. This is one of the basics of Object Oriented Programming. The Classes are the templates for the real world objects we see and using those templates we can create objects.

View – The view is nothing but the presentation layer. The data from the model is presented out to the user using the UI elements in views. The changed data is sent back to the server – a controller – action which updates the model.

Controller – The Controller does the role of mediating between the view and model. It chooses which request is mapped to which model and which view must be rendered for a certain request.

ASP.NET web forms:

The WEB Forms platform was based on the event driven programming model.  This was the programming model for the desktop applications, where the program code will be executed based on the user's actions - events. In case of web applications there is a catch, The client and server are disconnected. The ASP.NET Web forms framework did a lot off work internally to hide this, each event like a select in a drop down or button click or choosing a radio button triggered a "POST Back" - a call to the server and the code got executed in the server side. These event information , state of the application - UI was maintained and by the ASP.NET web forms framework. This was easy for developers to code, the "feel" of application development for web was similar to that of desktop. But this had its own disadvantages. Tightly coupled code. The UI Logic was tightly coupled with the Business Logic code. This rendered the code less testable. Each time when an event happened on client side the entire page was sent back to the server side which made the web applications slow. With the advent of AJAX the entire landscape of web applications changed.  The ASP.NET WEB Forms was based on the RAD Principles – Rapid Application Development. The ASP.NET WEB Forms follows the Page Controller Pattern. Every page has a code behind file that code behind file acts as a controller. The developers have very little control over the HTML Generated. Viewstate is a concept used in ASP.NET WEBForms, every control has a viewstate. The viewstate is used to maintain the state of controls in a WEB Page. The ASP.NET WEBForms serves the Pages, for each URL a physical page must exist.

ASP.NET MVC:

ASP.NET MVC is a framework that is built on the Model View Controller pattern.  The ASP.NET MVC follows the front controller flavor of MVC architecture. Controllers are responsible for responding to the user input, they are concerned with the flow of the application, working with data coming in and updating the corresponding model and providing the relevant view. Each request or action (like submit) is mapped to a controller – Action method. The MVC delivers the result of series of method call’s. The ASP.NET MVC is a framework Maps Routes to methods. The routes are based on the Controller’s and Actions. The framework supports the Dependency Injection and Inversion of Control.The traditional 3 tier architecture is linear, but MVC Architecture is triangular. The View sends the update to the controller, the controller updates the model, and Model directly updates the View. The 3 tier architecture is linear, it requires all the request’s to pass through a middle tier. The MVC Framework is a lightweight more testable framework. It does not  have a viewstate, It provides more control to the developers over the HTML that is served.