Reader small image

You're reading from  Architecting ASP.NET Core Applications - Third Edition

Product typeBook
Published inMar 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781805123385
Edition3rd Edition
Languages
Right arrow
Author (1)
Carl-Hugo Marcotte
Carl-Hugo Marcotte
author image
Carl-Hugo Marcotte

Carl-Hugo Marcotte is a software craftsman who has developed digital products professionally since 2005, while his coding journey started around 1989 for fun. He has a bachelor's degree in computer science. He has acquired a solid background in software architecture and expertise in ASP.NET Core through creating a wide range of web and cloud applications, from custom e-commerce websites to enterprise applications. He served many customers as an independent consultant, taught programming, and is now a Principal Architect at Export Development Canada. Passionate about C#, ASP.NET Core, AI, automation, and Cloud computing, he fosters collaboration and the open-source ethos, sharing his expertise with the tech community.
Read more about Carl-Hugo Marcotte

Right arrow

Model-View-Controller

This chapter delves into the Model-View-Controller (MVC) design pattern, a cornerstone of modern software architecture that intuitively structures your code around entities. MVC is perfect for CRUD operations or to tap into the advanced features unavailable in minimal APIs.

The MVC pattern partitions your application into three interrelated parts:

  • Models represent our data and business logic.
  • Views are the user-facing components.
  • Controllers act as intermediaries, mediating the interaction between models and views.

With its emphasis on the separation of concerns, the MVC pattern is a proven pattern for creating scalable and robust web applications. In the context of ASP.NET Core, MVC has provided a practical approach to building applications efficiently for years.

While we discussed REST APIs in Chapter 4, this chapter provides insight into how to use MVC to create REST APIs. We also address using Data Transfer Objects...

The MVC design pattern

Now that we have explored the basics of REST and minimal APIs, it is time to explore the MVC pattern to build ASP.NET Core REST APIs.

MVC is a design pattern commonly used in web development. It has a long history of being used to build REST APIs in ASP.NET and is widely used and praised by many.

As previously established, this pattern divides an application into three interconnected components: the model, the view, and the controller. A view in MVC formerly represented a user interface. However, in our case, the view is a data contract that reflects the REST API’s data-oriented nature.

Dividing responsibilities this way aligns with the Single Responsibility Principle (SRP) explored in Chapter 3, Architectural Principles. However, this is not the only way to build REST APIs with ASP.NET Core, as we saw in Chapter 5, Minimal APIs.

The new minimal API model mixed with the Request-EndPoint-Response (REPR) pattern can make building...

Using MVC with DTOs

This section explores leveraging the DTO pattern with the MVC framework.

This section is the same as we explored in Chapter 5, Minimal APIs, but in the context of MVC. Moreover, the two code projects are part of the same Visual Studio solution for convenience, allowing you to compare the two implementations.

Goal

As a reminder, DTOs aim to control the inputs and outputs of an endpoint by decoupling the API contract from the application’s inner workings. DTOs empower us to define our APIs without thinking about the underlying data structures, leaving us to craft our REST APIs how we want.

We discuss REST APIs and DTOs in more depth in Chapter 4, REST APIs.

Other possible objectives are to save bandwidth by limiting the amount of information the API transmits, flattening the data structure, or adding API features that cross multiple entities.

Design

Let’s start by analyzing a diagram that expands MVC...

Summary

This chapter explored the MVC design pattern, a well-established framework in the ASP.NET ecosystem that offers more advanced features than its newer minimal APIs counterpart. Minimal APIs don’t compete against MVC; we can use them together.

The MVC pattern emphasizes the separation of concerns, making it a proven pattern for creating maintainable, scalable, and robust web applications. We broke down the MVC pattern into its three core components: models, views, and controllers. Models represent data and business logic, views are user-facing components (serialized data structures), and controllers act as intermediaries, mediating the interaction between models and views.

We also discussed using DTOs to package data in the format we need, providing many benefits, including flexibility, efficiency, encapsulation, and improved performance. DTOs are a crucial part of the API contract.

Now that we have explored principles and methodologies, the following two...

Questions

Let’s look at a few practice questions:

  1. What are the three components of the MVC design pattern?
  2. What is the role of a controller in the MVC pattern?
  3. What are DTOs, and why are they important?
  4. How does the MVC pattern contribute to the maintainability of an application?
  5. How does attribute routing work in MVC?

Further reading

Here are some links to build on what we have learned in the chapter:

Answers

  1. The three components of the MVC design pattern are models, views, and controllers.
  2. In the MVC pattern, a controller acts as an intermediary, mediating the interaction between models and views.
  3. We use DTOs to package data into a format that provides many benefits, including efficient data sharing, encapsulation, and improved maintainability.
  4. The MVC pattern contributes to the maintainability of an application by separating concerns. Each component (model, view, controller) has a specific role and responsibility, making the code easier to manage, test, and extend.
  5. Attribute routing in MVC maps an HTTP request to a controller action. These attributes decorate the controllers and the actions to create the complete routes.

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Architecting ASP.NET Core Applications - Third Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781805123385
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Carl-Hugo Marcotte

Carl-Hugo Marcotte is a software craftsman who has developed digital products professionally since 2005, while his coding journey started around 1989 for fun. He has a bachelor's degree in computer science. He has acquired a solid background in software architecture and expertise in ASP.NET Core through creating a wide range of web and cloud applications, from custom e-commerce websites to enterprise applications. He served many customers as an independent consultant, taught programming, and is now a Principal Architect at Export Development Canada. Passionate about C#, ASP.NET Core, AI, automation, and Cloud computing, he fosters collaboration and the open-source ethos, sharing his expertise with the tech community.
Read more about Carl-Hugo Marcotte