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 class pulls a room entity object 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 manual mapping code for each resource. This boiler played code can be reduced by using a package like auto mapper. Let's install auto mapper in the project from the package manager. Search for auto mapper, and install it in the project.
To define how entity objects are mapped to their corresponding resource objects, auto mapper uses a class called a profile. I'm gonna create a new folder in the project called infrastructure. And, in here, I'll create a class called mapping profile. This needs to inherit from the profile base class that comes from auto mapper, so we'll import that name space. And, then in the constructor, for mapping profile, we'll say create map from room entity.
We'll need to import that name space to the room resource object. We'll map for member and the destination. The room resource.rate. We'll say that this needs to map from the source.rate divided by 100.0m. This is the same conversion code we were using in the room service. It just looks a little bit different.
We also need to do something like url.link, here, but we're not quite there yet. We didn't have to specify anything for the name property since it's mapped automatically. Properties, where the property name and type match, are just copied over automatically. Now, we need to tell asp.netcore to use auto mapper. We'll need to grab one more package from NuGet. This one's called AutoMapper.Extensions.Microsoft.DependencyInjection. We'll install that one.
Over in the start up class, let's open up configure services. And, somewhere in here, we'll say services.AddAutoMapper, pull in the auto mapper name space, and in the options we'll say we want to add the profile, that we just created, which is called mapping profile. Pull this in from infrastructure. Alright, there's one more thing we need to do. In room service, we need to inject IMapper, which is the auto mapper interface.
Pull in Imapper, here, from auto mapper. Call this _mapper. We'll inject it in the constructor, as well. And, down here in the service class, we don't need to do any of this manual copying, anymore. We'll just say return_mapper.map to room from the entity object.
Let's see if everything's working still. The response looks the same because we haven't actually changed anything, but the code is now cleaner and more maintainable. The returned resource href is still null because there is no way to access url.link from auto mapper, or from our service. In the next chapter, we'll refactor the way links are generated to fix this problem.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization
Skill Level Intermediate
1. REST API Concepts
2. Build a Basic API
3. Versioning and Errors
4. Secure the API
5. Represent Resources
6. Represent Links
7. Represent Collections
Add pagination7m 37s
8. Sorting Collections
9. Searching Collections
10. Forms and Modifying Data
11. Caching and Compression
12. 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.