In the early stage of Web development, such as ASP age, the front end HTML code was merged with Server Processing Script, usually VBScript, and Client-Side script, usually, JavaScript together.
When web development evolved into .NET age, the ASP.NET Web Application separates a .ASPX page and a Page behind (C# or VB code). This efficiently separates the client-side development and server-side processing. However, in the code behind part, it still mixes the data (and business logic) with front-end demonstration action together.
For example,
- protected void txtAccount_TextChanged(object sender, EventArgs e)
- {
- txtAccount.Text = account.Amount.ToString();
- }
For this code,the front end format and back end data is mixed together with operations:
- on the left, txtAccount.Text is to assign value to a front end text box (format), while
- on the right hand, it is an account class to bring the data of Amount in.
- Combining the back end data with front end demonstration together, we could view it controlled by a "controller".
The code behind is only for data binding or presentation purpose, it should not include any business logic. However, developer is easier to put some business related code into code behind, such as
- protected void txtAccount_TextChanged(object sender, EventArgs e)
- {
- if(account.Amount > 300)
- {
- txtAccount.Text = account.Amount + ............;
- }
- else
- {
txtAccount.Text = account.Amount + ............;
- }
- }
finally, make a "broad presentation layer" mixing business logic and presentation operation together.
Previously, what we did is to split this too "broad presentation layer" into two: presentation and application logic. Then we have so-called three tier architecture. I would say, the three tier architecture is a separation of content, in concept, we could say, it followed the MVC model, but not exactly MVC model that the controller was not clearly separated out. And furthermore, it was just a concept pattern, it is not built into the Visual Studio.
That is the reason, I believe, Microsoft introduced the ASP.NET MVC module into the Visual Studio, it makes developer work much easier: directly using the MVC module instead of making a lot of more work to develop some MVC similar one by themselves.
Finally, we will give a perfect example of the separation of data and view. We know, Visual Studio Web API module is based on MVC module, and similar to MVC module without the VIEW. Actually, there IS a view (format). Let us examine the default Web API Core Web API, the output could be in JSON format by, say, Firefox browser:
Or could be Text format like this,
They are actually the VIEW of the Web API, i.e., the format with data, that is separated from the data and controller.
Summary
This article discussed several aspects of MVC design pattern with understanding and examples.