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.

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

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:

With AutoMapper

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:

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; }
}

Notice that we removed the Password field.

This is how DTO helps in securing your application.

Step 3: Create Mapping Profile

A mapping profile is where you define how one object maps to another.

using AutoMapper;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<User, UserDto>();
    }
}

What this does

Step 4: Register AutoMapper in Application

In .NET 6 or later, you register AutoMapper in Program.cs.

builder.Services.AddAutoMapper(typeof(MappingProfile));

Why this is needed

Step 5: Use AutoMapper in Service or Controller

Now you can use AutoMapper wherever needed.

using AutoMapper;

public class UserService
{
    private readonly IMapper _mapper;

    public UserService(IMapper mapper)
    {
        _mapper = mapper;
    }

    public UserDto GetUser()
    {
        var user = new User
        {
            Id = 1,
            Name = "John",
            Email = "[email protected]",
            Password = "123456"
        };

        var userDto = _mapper.Map<UserDto>(user);
        return userDto;
    }
}

What is happening here

Before vs After Using AutoMapper

Without AutoMapper

var userDto = new UserDto
{
    Id = user.Id,
    Name = user.Name,
    Email = user.Email
};

With AutoMapper

var userDto = _mapper.Map<UserDto>(user);

Key difference

Advanced Mapping Scenarios

Sometimes real projects are not that simple.

Custom Mapping

CreateMap<User, UserDto>()
    .ForMember(dest => dest.Name,
               opt => opt.MapFrom(src => src.Name.ToUpper()));

Here, we are modifying data during mapping.

Ignoring Properties

CreateMap<User, UserDto>()
    .ForMember(dest => dest.Email, opt => opt.Ignore());

This is useful when you don’t want certain fields in output.

Real-World Use Case

Let’s say you are building an e-commerce application.

Using AutoMapper:

Advantages of AutoMapper

Disadvantages of AutoMapper

When Should You Use AutoMapper?

Use it when

Avoid it when

Summary

AutoMapper makes object-to-object mapping in .NET simple, clean, and efficient. Instead of writing repetitive code, you define mapping rules once and reuse them throughout your application. By using DTOs along with AutoMapper, you can protect sensitive data, improve performance, and keep your code easy to manage. Start with basic mapping and gradually explore advanced features as your project grows, and you will see a significant improvement in both development speed and code quality.