What is MVC (Model View Controller)
Before taking you into the programming side of asp.net MVC let’s first make you understand what MVC is. Is MVC something that was developed as a part of .net programming by Microsoft, or was it developed as a part of Java programming by Sun. We need to get and handle on MVC in general, what it is and what is its importance.
MVC – An architectural pattern
MVC stands for Model View Controller. Is it a programming technique ? or is it a framework work ? Neither is it a programming technique and nor is it a framework. Then what is it ?
Many put MVC under the design pattern category. But it’s not a design pattern, rather its an architectural pattern. Let’s first understand the difference between a design pattern and an architectural pattern:
Design pattern – According to Wikipedia “a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.”
Architectural pattern – According to Wikipedia “Architectural patterns are software patterns that offer well-established solutions to architectural problems in software engineering. It gives description of the elements and relation type together with a set of constraints on how they may be used. An architectural pattern expresses a fundamental structural organization schema for a software system, which consists of subsystems, their responsibilities and interrelations. In comparison to design patterns, architectural patterns are larger in scale. ”
Now that we are clear with what a design pattern is and what an architectural pattern is, let’s try and understand why MVC is an architectural pattern and not a design pattern. We use MVC for shaping complete applications and not small parts of an application. It’s an end to end solution for the whole application. It helps you design an application in a way that we can separate the Data, Logic and Presentation in a clean manner. Thus we put MVC under the architectural pattern category. Now that we understand that MVC is an architectural pattern, we need to understand how we use it.
MVC is not something that is particular to asp.net. Any language with enough ability to support advanced level of programming can be used to implement MVC pattern and code an application. Thus you can have MVC pattern implemented using PHP, JAVA or any other present day language. Now let’s understand what MVC and how does it help us in designing an application. Below is an pictorial representation of the MVC pattern:
In the figure above we can clearly see a Model , View and a Controller. Lets now define all these three and understand their role in an application.
Model:
A model is responsible for holding the data on which the application works. It may be raw data that is being fetched from a database or processed data. Any operations of the data that are being performed by the application are done by a Model.
View:
This is the presentation layer. This is the layer which is presented to the user for interaction. There can be multiple views for a given Model. The major responsibility of the View is to present the data to the user. No data base operations or data manipulation operation should be carried on inside the View.
Controller:
This is the glue layer between a Model and a View. A controller is responsible for receiving the input from the user when he / she interacts with the view and initiating the response cycle for the input.
The asp.net MVC 4.0 framework that we installed on our host provides us with required classes for creating the Models, Views and Controllers. We do not have to write the MVC framework of our own. All that we have to do is to use this MVC framework provided by asp.net and create our own applications. In fact Orchard provides a Comand Line Code-Generator which builds a complete Module or Theme structure simply by giving it a new name...
Engines:
In 2010 MSN introduced a new View Engine called Razor within it's release of ASP.NET MVC 3 Framework and the Web Forms view engine. Both view engines are part of the MVC 3 framework. By default, the view engine in the MVC framework uses Razor .cshtml
and .vbhtml
, Web Forms .aspx
pages to design the layout of the user interface pages onto which the data is composed.
Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or Visual Basic .NET programming languages. Razor was in development in June 2010[1] and was released for Microsoft Visual Studio 2010 in January 2011.[2] Razor is a simple-syntax view engine and was released as part of ASP.NET MVC 3 and the Microsoft WebMatrix tool set.