Topics Covered

MVC Framework

Model View Controller (MVC) is simply a framework for designing web applications, as simple as its definition could be.

The three layers are interconnected and are dependent on each other. The following figure will explain that better:

mvc

As we can see in the preceding, the Data layer or the Model that deals with the data is accessed both by the View and the Controller. Here, in MVC there is a separation between every layer, that helps in loose coupling the items and also in the application development process. Now, one might wonder what exactly Separation of Concerns means! This can be framed as there should not be or there should be a minimum of overlapping/dependency of functions in an application. For example, take the scenario of the View and the Controller. Can they be separately done? That is, can their be no dependency among them? The View is the presentation layer and the Controller is the action layer. We can have in a real scenario a controller without a View, but a view without a controller sounds a bit radical. This is not necessarily always true. So, we can conclude here that Separation of Concerns can be minimal but there still lies a Grey area. Note: Thus always have a separate UI layer, Data Access Layer & Logic Layer to get clean separation.

MVC DEMO

The preceding image displays the simple MVC folder structure. The Models having the Entities, Controllers as well as the Views for each controller, the shared folder having the master pages.

Lets get

Let's get into more details.

Models

Models (in a bit more depth): Contains all the application logic, in other words:

This holds as well as manipulates the data in the database. This folder contains the classes/entities that are required and present in the application.

Views

Views (in a bit more depth): The View generally stores the HTML files that may be .cshtml or .vbhtml (C# and VB) respectively. These may have extensions as html, asp, aspx. The interesting thing is that for every controller there is a view folder and for every action a view. There is always a Shared folder inside View, that is used to have the Layout or the Master pages.

Controllers

Controllers (in a bit more depth): This contains the controller classes responsible for input and responses. The name of the Controller ends with Controller to distinguish. For example, "HomeController.cs". Every controller implements the Icontroller interface from the System.Web.Mvc namespace.

  1. public interface Icontroller {
  2. void Execute(RequestContext requestContext);
  3. }
The single method Execute is invoked when a request is targeted at the controller class. What happens to the request that comes to the application? The request that comes to the web application first goes through the UrlRoutingModule object (System.Web.Routing.UrlRoutingModule). The request gets parsed and then the route selection is done. When the application starts up, it runs the Application_Start() method present in the Global.asax that marks the application initialization. Pipeline In MVC Conclusion

This was the basic and foremost things to understand about MVC. These are all done on the background. Being developers, not bothering about the flow is now worthy enough. Thus, just adding an action and obviously a View to the Controller does not mean we have done something great!! You would always prefer a pot full with water rather than the one that is nearly empty!

happy

We are done with the first part. We will be covering interesting topics for learning MVC in Part 2 I hope this helps at least. Any suggestions, queries and comments are most welcome.