top of page
Writer's pictureThe Tech Platform

AutoMapper Extensions in .Net Core



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 :














OrderDetails.cs



Destination DTO Class:

CustomerDetails.cs



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



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



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



Step 4 : Create mapper using CreateMapper() as below


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



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


OrderResponse.cs


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



This completes AutoMapper implementation in .Net Core application :)



Source: Medium- by Karthik


The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page