top of page

AutoMapper Extensions in .Net Core


ree

In C# mapping of one POCO class object to another POCO class object can be achieved by assigning source property to destination property one by one.


Source DTO class :

ree













OrderDetails.cs



Destination DTO Class:

ree

CustomerDetails.cs



ree

OrderDetailResponse Method


In above code , line number 16 to 19 is were we are mapping the data from source to destination object property one by one.

To avoid mapping each property one by one , we can use AutoMapper package. Using this package we can define source object to destination object and this avoids developers responsibility. Code can be in readable format and code looks clean.

Step 1: Install Automapper package from nuget as below


ree

Step 2: In OrderResponse class we will declare IMapper interface using Automapper package.


ree

Step 3 : Inside OrderDetailResponse method we can define source and destination DTO class using MapperConfiguration method from AutoMapper reference.


ree

Step 4 : Create mapper using CreateMapper() as below


ree

Step 5 : Now we are mapping source data to destination data using Map


ree

In above code line number 27 we are mapping data to destination object

By using Automapper as above we have avoided mapping each properties one by one.

After using Automapper package , OrderResponse.cs code will like below


ree

OrderResponse.cs


Step 6: Add below code to register AutoMapper inside ConfigureServices method of Startup class.

ree


This completes AutoMapper implementation in .Net Core application :)



Source: Medium- by Karthik


The Tech Platform

Comments


bottom of page