In this tutorial, we are going to cover Asp.Net Core 3.0 Automapper implementation using a very simple example. Automapper is a very popular Object-to-Object mapping library that can be used to map objects. So, using automapper, we can map one object to another object throughout our application with just a little bit configuration. So, first-of-all, we will create a new Asp.Net Core project in Visual Studio 2019 and then we will install AutoMapper and then we will configure AutoMapper in our project and then we will add model class and a Dto (Data Object Mapper) class and then we will map both (Model class and Dto) and then finally we will see the output.
Previous Tutorials
- Asp.Net Core 3.0 Web API Versioning Best Practices
- Asp.Net Core 3.0 Web API Token Based Authentication Example using JWT
What will you learn in this tutorial?
- How to create Asp.Net Core 3.0 application?
- How to Install Automapper in Asp.Net Core 3.0 application?
- How to Configure Automapper in Asp.Net Core 3.0 Application?
- How to map objects?
How to use automapper in Asp.Net Core 3.0 application
Let’s see how to use automapper in Asp Net Core 3.0 application using a very simple example.
How to Create Asp.Net Core 3.0 Application in Visual Studio 2019?
In this step, we will install Asp.Net Core 3.0 application. So, go to visual studio 2019 => then click on “Create a new project” button => and then choose “ASP.NET Core Web Application” template => and then click on “Next” button and then enter the name of project in “Project name” field and then choose “Location” and then click on the “Create” button.
Now, select “Asp.Net Core 3.0” framework from the drop-down and then choose “Web Application (Model-View-Controller)” template and then click on the “Create” button as you do see below in the screenshot. It will create a new Asp.Net Core 3.0 project.
How to Install AutoMapper in Asp.Net Core 3.0 Application
Now, in this step, we will install AutoMapper in our project. So, go to visual studio and then go to “Tools” from the menu and then select “NuGet Package Manager” and then choose “Manager NuGet Packages for Solution..”. Now, go to “Browse” tab and then install this below package as you do see below in the screenshot.
Or you can install this package by using this below command in Package Manager Console as you do see below in the screenshot.
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
How to Configure Automapper in Asp.Net Core 3.0 Application
In this step, we will see how to configure automapper in Asp.Net Core 3.0 application. So, go to project folder structure, and then go to Startup class and then add some changes as you do see below in the code file’s Line # 11 and Line # 26.
Create a Model Class and Dto (Data Transfer Object) Class
Now, in this step , we will create two classes. One is Model class and the other one is Dto(Data Transfer Object) class. So, go to Models’ folder and then right click on the folder name and then add a new class with the name of “Employee” and then write some properties as you do see below in the code.
Now, again add a new class with the name of “EmployeeDto” as you do see below in the file.
How to add relation of Employee class with EmployeeDto class?
Now, in this step, we will see how to add relation between a domain class and a Dto class. So, again add a new class (E.g. AutoMapping.cs) and then write some code as you do see below in the code.
Let’s understand the above code.
Line # 9: In this line, we are inheriting AutoMapping class from Profile.
Line # 13: In this line, we are mapping our Employee and EmployeeDto Classes.
How to map Employee class with EmployeeDto Controller?
Now, in this step, we will see how to map Employee class with EmployeeDto class within the Home Controller. So, go to HomeController and then go to Index method and then write some code as you do see below in the file.
Let’s understand the above code.
Line # 15 to 19: In this block of code, we are injecting IMapper interface. This will help us to use our configured mappings.
Line # 22 to 25: In this block of code, we are initializing Employee object.
Line # 26: In this line, we are using IMapper interface that we have injected into our constructor to call the Map method. And we are giving the type that we want to map and the object that we want to map from.
Line # 27: In this line, we are returning the EmployeeDto object to the view.
Now, go to Index view and access the returning values from the Index method as you do see below in the file.
Now, run your project by pressing f5 and then you will see the output as you do see below in the screenshot.
Thank you for reading. Please keep visiting and sharing within your community.
Leave a Reply