Keeping data models separate from binding and response models can lead to a lot of manual property mapping. Learn how to automate this mapping using open-source tools.
- [Instructor] The default room service pulls a room entity from the data context, and then creates a new room resource object by manually copying over the properties like name and rate. As we add more resources to the API, it will mean more of this mapping code for each resource. This boilerplate can be reduced by using a package like AutoMapper. AutoMapper is a NuGet package we can install using the NuGet Package Manager. I need to stop my project first. I can search for AutoMapper and install it into my project.
To define how database entity objects are mapped to their corresponding resource objects, AutoMapper uses an object called a profile. I'll create a new class in the Infrastructure folder called MappingProfile. This class will inherit from an AutoMapper base class called Profile. In the constructor of our class, we'll define which properties get mapped over between entity objects and resource objects. We'll create a map for our RoomEntity class, which we need to import, and targeting the Room resource class.
We can define a configuration on a per member or per property basis. In this case, we want to define some special handling for the rate property first. Remember, it needs to be divided by 100 and cast to a decimal. We'll do that by specifying we want to work on the Rate property on the target object, and then map that from the rate property on the source object, and we can divide by 100.0m to cast it to a decimal. We'll make a note here that we also need to handle the Href property, but for now, we'll leave it off.
The name property is mapped automatically, because it's a simple case where the property names match and the types match. Next, we need to tell ASP.NET Core to use AutoMapper. To do this, we need to install one more NuGet package in the project. This time, we need to look for automapper.extensions and install the dependency injection extension. With that package installed, we can go to the start up class, and somewhere in ConfigureServices, we can say services.AddAutoMapper.
We may have to import that name space. Now that we have that wired up, we can go to our default room service and reduce some of this code. Instead of building up a room resource manually, we can simply say return Mapper, which we need to import from the AutoMapper name space, .Map, and give it the target, which is type Room, and the source, which is the entity object we pulled out of the database. This code is much cleaner and it'll accomplish the same thing. We can test it out in POSTMAN, just to make sure it still works.
We should expect to see the exact same properties come through over the wire when we send this request. That's what we have. The returned resources Href is still null, because there's no way to access URL.link from AutoMapper. Next, we'll refactor the way links are generated.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Deploying ASP.NET Core Applicationswith Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.