In this video, learn the advantages of using the MVC pattern, including separations of concerns. Also, learn how ASP.NET brings those benefits of the MVC pattern, and some other unique ones to developing web applications.
- Using a pattern like MVC has many advantages, which actually make you a better developer. Make your application easier to maintain and create, and help teams work together more efficiently. Separation of concerns is the practice of breaking your code into distinct responsibilities or concerns, that overlap in functionality as little as possible. For example on MVC, that means that the view doesn't care where the model comes from or how it works. Its only concern is how to properly display that model to the user. The controller on the other hand, knows how to load the model from the data base and how to route a request at the correct view, but nothing else. Each piece handles its own thing. Because the MVC pattern is so well organized, MVC applications tend to be easier to maintain. Thanks to separation of concerns, we don't have to worry about display logic and application logic being mixed together in the code. And also we can quickly and easily find where existing code is located, to update it, because the pattern is so prescriptive about where things should go. For example your user authentication code isn't likely to get mixed up with your order processing code. Since they're likely to be in very different places, models and controllers, to satisfy the requirements of the MVC pattern. By separating our concerns, we also allow ourselves more easily to reuse code in our application. For example, if two different screens in the application display an order model, we can use that same order model in both views. Without having to worry about that code being duplicated business logic throughout our application. The MVC pattern also encourages two important things to a software engineer. Low coupling and high cohesion. Low coupling means that the code in different areas is independent of the code in other areas. And high cohesion means, that related code can be placed together. This is something that happens very naturally when you follow the MVC pattern. Finally the MVC pattern allows teams to work together more efficiently. Source control systems such as GIT, allow developers to work on code files at the same time and to merge those changes together later. But by separating code into into different features and different areas of concern, MVC allows teams to work in different areas of the application, without the need to make changes to the same files quite as often. ASP.NET MVC implements the MVC pattern and therefore brings all of those advantages to the table. In addition, it has some of its own framework specific advantages. For example unlike some HTML generation frameworks, ASP.NET MVC gives you full control over the exact HTML that it generates, allowing for very fine control over the rendering of your application. The naming and folder organization convention of ASP.NET MVC also helps keep your application organized and enforced separation of concerns. By keeping your logic separate from the presentation, MVC makes it easy for you to create uni tests for your application, without worrying about the specifics of the user interface. It was also built to embrace the stateless nature of the web and the http protocol. This makes it easy to avoid making bad architectural decisions that are possible in other frameworks which try to hide the stateless nature of the web from you. Finally, ASP.NET MVC allows you to create more human readable URLS that aren't dictated to you by the way that you've organized your code files. These URLS also allow for better search engine optimization or SEO for your application.
- Creating a new ASP.NET MVC 5 project
- Using controllers and action methods
- Creating attribute routes
- Strongly typed models
- Building HTML with HtmlHelpers
- Sharing views
- Building models with Entity Framework
- Unit testing code