MVCModel View Controller (MVC) is a software architecture pattern used to separate an application into 3 layers data, business logic, and user interface (UI). The MVC pattern is not new but has only been officially part of the ASP.NET since 2009.  Over the past few years the MVC pattern has exploded with popularity around the Web Development community. Many other popular frameworks use this pattern:

  • Ruby
  • Laravel(PHP)
  • CodeIgniter
  • CakePHP
  • FuelPHP

As result of the popularity of these frameworks and to answer the request from developers in the ASP.NET community; Microsoft developed their own brand of MVC. The legend is that Scott Guthrie developed the concept on a paper napkin during a plane ride. Since it’s release ASP.NET developers have been excited to use MVC and it is now one ASP.NET’s most popular frameworks. Many features in the last few Visual Studio releases have catered to MVC developers versus Web Forms. Just like the other popular MVC frameworks, ASP.NET is open-source and community driven.

keep your models skinny, controllers fat and views simple

Using the MVC pattern allows for seperation of concern, the main idea here is the developer can separate content from design. The View does not need to worry about how or where the data comes from it just assumes the it will always be there. Same principles apply to how the controller handles user input, it’s doesn’t matter to the view or model what the user inputs because that can be handled by the controller.

How Does MVC Work?

Like I mentioned above the idea behind the MVC pattern is to allow for separation of concern. So if we look at the at what each layers represents we can get a better picture of what of what is going on at a higher level.

Model

Data layer or database layer. This layer is where you will create a Class for each of your data models, typically each class will represent a single table in your database.

View

Presentation layer of the user interface. This layer will contain your HMTL/JavaScript/Razor logic. Information presented in this layer is passed from the model through the controller. Passive layer that does none of the processing; often refereed to as the design layer.

Controller

Business logic layer. This layer does most of the heavy lifting on the application, the controllers are represented as Classes that interact with other objects(models, views, etc). Handles interaction between model and view.  Receives events from view and processing those events. Returns view to the user.

Example

Keeping in the mind the 3 layers, lets take an example and apply MVC pattern to it. Suppose we want to create an application to track books we have read.  Just a simple application that will list books we have read and allow us to create, edit, and delete books.
Model
For this example a Book will represent our model. Each book will have an id, name, date read, review, author, description, quotes, and rating.
So in our Book Class we will declare each of these and add getters and setters. 

Controller

Our application will need the ability to create, delete, see book details, edit, and list of books. We can create one class that will handle all of these actions. Looking at the code below the index action on line 20 is our list feature, it will be served as the index page that simply lists all books in the database. Each ActionResult corresponds to a page view we serve up to the user.

View

The model and controller have both been Classes with a .cs extension but our views will be a Razor view with a .cshtml extension. These .cshtml pages are similar to the .aspx views-bookpages in Web Forms. All of our design decisions can be done here with little need to worry about development concerns. We have already coded our Controller to allow for a create, delete, details, edit, and index Views now we just need to create the views for each. Here is a screenshot of those Views inside View/Books directory of our application and the code for the index page. Notice how there is clean the code looks for the View.

 

The full code for the BookTracker Application can be found at Github https://github.com/tmhenson/booktracker. Just in this quick example you can start to see how ASP.NET MVC is a powerful framework for web development. While there is more MVC than just the separation of concern, it is by far one of my favorite features about the pattern. If your like me and come from a Web Forms background you can definitely see how much cleaner the code is using a MVC pattern. Feel free to tell me what you think in the comments below.