Solution
This article is the continuation of the How to create asp.net core API’s article, So I highly encourage you to see the article understand the source code file structure of the project. This article is for how to configure Auto-Mapper for asp.net core applications. So, without you going through the below article you are probably not able to understand this article and I started using this auto mapper library to the asp.net Core Web API project. You can follow the same approach for any other like traditional Asp.net MVC, Windows, etc. Projects.
Did you ever think of object-object mapping?
When I was a beginner at the asp.net core and entity framework core concepts, and when I was started to use this framework while saving the data or fetching the data from the asp.net core entity framework. converting Database Entities or POCO object data to user View model Object data, I used a traditional foreach loop, creating
a new instance of View model object to assign the data values to ViewModel Object and it was disgusting sometimes because of several loops.
To solve the issue we have one good package in our asp.net core, i.e Auto Mapper
What is Auto-mapper in asp.net core?
In simple words, Auto-mapper helps us to copy data from one type of object to another type of object. in our case database entities results into our model object, that’s it. most of the developers like me also get bored while mapping/converting database results Entites object to custom view object entities, This type of code is rather tedious and boring to write, so why not leave that job to this Auto-Mapper?
Now, It’s time to configure Auto-mapper to our asp.net core web API Project
Step 1: Install Auto-mapper and dependency Package
Add below two packages to the project library so AutoMapper is a simple little library built to solve a deceptively complex problem – getting rid of code that mapped one object to another.
Create dto classes and POCO classes in respective folder as showed in the below images.
Step 2: Create Mapping Profile class
Profiles allows the developers to organize maps into classes, improve the code readability and maintainability. developers may create any number of profiles can be created, and added to one or more configurations as needed.
In the Below image categoryProfile.cs by inheriting the profile from auto mapper
Step 3: register Auto mapper service servceCollection container
Register the the Mapper profiles service by using services.AddAutoMapper() in ConfigurationServices method of the startup.cs file as showed in the below image.
Step 5: Add the mapper dependency in controller
Now developer can use the mapper functionality in our class to copy the data from entity class to ViewModel or dto class without using anyc# loops.
Summary
As we discussed in above section Auto-mapper helps us to copy data from one type of object to another type of object. in our case database entities results into our model object, that’s it. most of the developers like me also get bored while mapping/converting database results Entites object to custom view object entities