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 - ASP.NET Core Tutorial
From the course: Building and Securing RESTful APIs in ASP.NET Core
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Create a resource class2m 19s
-
(Locked)
Load data from configuration1m 35s
-
(Locked)
Return data from a controller3m 5s
-
(Locked)
Set up an in-memory database2m 22s
-
(Locked)
Create data model classes1m 44s
-
(Locked)
Seed the database with test data5m
-
(Locked)
Return a resource from a controller4m 47s
-
(Locked)
Move data access to a service4m 39s
-
(Locked)
Map models automatically3m 47s
-
-
-
-
-
-
-
-
-