Join Jess Chadwick for an in-depth discussion in this video The Model-View-Controller architecture pattern, part of ASP.NET MVC 4 Essential Training.
The last few videos give a high level tour of the major components that make up an ASP.NET MVC website project. What those videos only briefly mentioned, however, are the fundamental concepts that drive development of an ASP.NET MVC application, the Model-View-Controller or MVC architectural pattern. Chapter 1 starts out with a brief overview of this pattern, but in this video we'll really dive into what makes up the MVC Pattern. Chapter 1 left us with a picture of three separate and distinct layers, the Model, the View, and the Controller, and mentioned that each of these layers has their own specific responsibilities.
Let's take a look at what those responsibilities really are. The Model layer contains all of the application's business logic, the code that describes how your business works. In our online auction website, for example, our model will consist of things like auctions, bids on those auctions, and even the users that place those bids. These classes usually include behaviors as well such as how users place bids and what should happen once those bids are placed. In an ideal world, the classes that make up the Model layer have nothing to do with the web.
It's the View layer's job to render anything that your application needs in order to create an immersive user experience. Finally, there's the Controller layer. This layer acts as the traffic cop, controlling the application flow and interaction between the Model and the View. Controllers are the brains of an ASP.NET MVC application, requesting data from the Model layer and choosing the correct method of getting that data to the user. Note that the Controller does not perform any of this work directly, its real job is to delegate as much as possible to the model and view layers so that they can do the real work.
For instance, when a user requests the details of an auction, the Controller asks the Model which data should be shown. Then it passes this data on to the View, which generates the HTML that gets sent back to the user's browser. What's important in this scenario is that the Controller is neither retrieving the data directly nor generating the HTML itself, it's merely coordinating the steps that need to occur in order to do so. The way the three layers of the MVC pattern interact with each other is a very deliberate attempt to achieve something called separation of concerns.
Following the separation of concerns philosophy the various components that make up an application are only responsible for doing one thing, remaining as ignorant as possible as to what the other components in the same application may be doing. Components that are built using true separation of concerns are inherently loosely coupled, meaning that they do not depend on any other components in order to get their job done. An application built using loosely coupled components creates a level of isolation that makes it very easy to enhance or even replace individual components with great confidence that none of the other components in the system will be directly affected.
The MVC architectural pattern can lead to some great benefits in developing robust and maintainable applications. However, I fully admit that the MVC pattern is a departure from some of the more established approaches and may not necessarily be right for every developer nor every project. So if you like the sound of a loosely coupled architecture and are eager to see the MVC pattern in practice, please continue with the course to find out how to fully leverage this pattern using the ASP.NET MVC Framework.
On the other hand, if you think this approach may not be right for you, I urge to at least review the next few videos to see the pattern and the benefits that it offers in action before deciding against it.
- What is Microsoft ASP.NET MVC?
- Creating your first ASP.NET MVC project
- Understanding the MVC architecture
- Interacting with models and ViewData
- Applying advanced layout techniques
- Routing requests to controller actions
- Validating user input
- Authenticating registered users
- Caching pages
- Reducing bandwidth usage with bundling and minification
- Displaying mobile-optimized content
- Simplifying a complex application with areas
- Configuring and deploying to IIS or Windows Azure