- [Instructor] The default room service pulls a room entityfrom the data context,and then creates a new room resource objectby 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 reducedby using a package like AutoMapper.AutoMapper is a NuGet package we can installusing 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 mappedto their corresponding resource objects,AutoMapper uses an object called a profile.I'll create a new class in the Infrastructure foldercalled MappingProfile.This class will inherit from an AutoMapper base classcalled Profile.In the constructor of our class,we'll define which properties get mapped overbetween 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 memberor per property basis.In this case, we want to define some special handlingfor the rate property first.Remember, it needs to be divided by 100and cast to a decimal.We'll do that by specifyingwe want to work on the Rate property on the target object,and then map that from the rate propertyon 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 handlethe 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 matchand the types match.Next, we need to tell ASP.NET Core to use AutoMapper.To do this, we need to install one more NuGet packagein the project.This time, we need to look for automapper.extensionsand 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 serviceand 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 objectwe pulled out of the database.This code is much cleanerand 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 propertiescome 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.

Resume Transcript Auto-Scroll

Author

Released

6/28/2017

Most people have heard of RESTful APIs, but the underlying concept—REST or representational state transfer—still causes confusion. REST is all about modeling resources and how those resources can change. RESTful APIs use this architecture along with HTTP requests to transfer data and changes in application state between clients and servers. This course will break down the principles of RESTful design and show how to build secure RESTful APIs on top of ASP.NET Core. Nate Barbettini answers questions such as: What is RESTful design? How do you perform RESTful routing? How can you build reusable classes to represent resources? What role does caching play? And how do you secure RESTful APIs? He also covers topics such as data modeling, hypermedia relationships, and authentication and authorization. By the end of the course, you should know not only the basics—how to properly use HTTP verbs and return JSON data from ASP.NET Core—but also the best practices for building secure and scalable APIs to serve web clients, mobile clients, and beyond.