Introduction
When you build applications using .NET, especially ASP.NET Core Web APIs, you often work with multiple layers like database models, business logic models, and API response objects. These objects usually look similar, but they are not exactly the same.
For example, your database model may contain sensitive data like passwords or internal fields, but your API response should not expose that information.
This is where DTOs (Data Transfer Objects) and AutoMapper come into the picture.
Instead of manually copying data from one object to another again and again, AutoMapper helps you do it automatically with clean and maintainable code.
In this article, we will understand everything step by step in simple words with practical examples so that even beginners can easily understand how DTO mapping works in .NET.
What is a DTO (Data Transfer Object)?
A DTO is a simple object whose main purpose is to transfer data from one layer of an application to another.
It does not contain any business logic. It only contains the data that you want to send or receive.
Real-life example
Think of ordering food online.
The kitchen (database) has full details like ingredients, cost, supplier, etc.
But what you see in the app (DTO) is only name, price, and description
You don’t need internal kitchen data, only what is relevant to you.
That is exactly what a DTO does in software.
Why DTOs are important
They protect sensitive data
They reduce unnecessary data transfer
They make APIs cleaner and faster
Why Use AutoMapper in .NET?
Now imagine you have 20 properties in a class. Writing mapping code manually for each property becomes boring and repetitive.
Without AutoMapper
You have to write code like this every time:
Assign each property one by one
Update mapping when model changes
Risk of missing fields or making mistakes
With AutoMapper
It automatically maps properties with the same names
You write mapping logic only once
Your code becomes shorter and easier to read
Real-world analogy
Manual mapping is like copying data from one notebook to another line by line.
AutoMapper is like taking a photocopy instantly.
Install AutoMapper in .NET Project
To use AutoMapper, you first need to install it in your project.
You can install it using NuGet Package Manager or command line.
Using Package Manager Console
Install-Package AutoMapper
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
These packages help you:
Use AutoMapper core functionality
Integrate it with ASP.NET Core dependency injection
Step-by-Step Example: Mapping DTO using AutoMapper
Let’s understand this with a simple and practical example.
Step 1: Create a Domain Model
This represents your database or core business object.
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
Here, Password is sensitive information and should not be exposed.
Step 2: Create a DTO
This is what you will send to the client (API response).
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

Join the conversation! Your thoughts help the community grow.