Model–view–controller (MVC) is a software design pattern for implementing user interfaces on computers. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.
View based solution for Action based requirement
Users come with a specific purpose to a website and they communicate their purpose by actions. These actions are communicated by button click , right click or through a browser URL etc. Due to this action based structure HTTP protocol was chosen for Web. The ASPX architecture itself was not fitting logically to the end user’s action based approach. In other words if the end user sends a “Buy” action it first comes to a view like “Shopping.aspx” who in turn kicks of “Shopping.aspx.cs” which executes a complicated page life cycle which in turn executes the action which will fulfill the request of the end user.
MVC architecture makes the flow more logical and clear. The first hit comes to an action which belongs in to a controller and then controller invokes the view with appropriate model.
Side effects of bad architecture: - Tight coupling
In Asp.net behind code which looks physically different in different files was never actually decoupled i.e. ASPX.CS cannot be separated from ASPX.
So if we can change the view first based architecture to action first based architecture then we can reuse the same action code with different views. For instance if an end user sends an action “Display” it can invoke “DisplayDesktop.aspx” or it can display “DisplayMobile.aspx” depending on the type of device.
HTML is not the only response type
Because of the tight coupling between view and code behind even the response type is fixed in web form , its by default HTML. If you wish to change it you need to play around with Content-type and “Response.End” methods etc which is quiet tedious.
If we create “Action” first structure then the action has all the luxury in the world to decide what kind of response type should go out. This makes our system more flexible in terms of same action with different outputs.
FlexibleCombination of view and data
When we send response to a user it’s a combination of view (display) and data (model).Webform is a view first architecture so the view decides which model to connect making the view NOT SO flexible and also involving view in complex decision making.
But if we make action first architecture then the first hits comes to the action and he picks the Model and the view to send different responses. In MVC action we can pick up the same model and attach it with different view.
Making behind code a normal class for unit testing
The behind code in web form is a very typical heavy and bulky weight partial class which cannot be instantiated in simple C# code straightforward.
For unit testing we can invoke the actions of the button click methods and test if the session variables are set properly,view states are properly etc. In MVC this becomes a normal class. A class which can instantiated in simple unit test project and you can test various aspects like session ,view bag , temp-data in an easy way.