From the course: Building and Securing RESTful APIs in ASP.NET Core

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Map models automatically

Map models automatically - ASP.NET Core Tutorial

From the course: Building and Securing RESTful APIs in ASP.NET Core

Start my 1-month free trial

Map models automatically

- [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…

Contents