AutoMapper

What is AutoMapper?

AutoMapper is a popular object-to-object mapping library in C# (and .NET) that helps automatically map one object type to another. It's especially useful when you need to convert between domain models and DTOs (Data Transfer Objects), or any time you need to reduce boilerplate code for copying values between objects.

What does AutoMapper do?

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

You can configure AutoMapper to do it automatically.

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

Why Use AutoMapper?

Example

//1. Define your models:
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}
public class UserDto
{
    public int Id { get; set; }
    public string Name { get; set; }
}
//2. Configure AutoMapper:
var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<User, UserDto>();
});
var mapper = config.CreateMapper();
//3. Use AutoMapper:
User user = new User { Id = 1, Name = "Alice", Email = "[email protected]" };
UserDto dto = mapper.Map<UserDto>(user);

Advanced Features

NuGet Installation

// Install it via NuGet:
Install-Package AutoMapper

// Or with .NET CLI:
dotnet add package AutoMapper

AutoMapper

Pros

Advantage Details
🔧 Less boilerplate Automatically maps matching properties by name
⏱️ Fast to implement Great for CRUD-heavy apps
🔁 Supports complex scenarios Nested mapping, value resolvers, flattening, reverse maps
♻️ Centralized config All mapping rules live in profiles—great for team-wide consistency

Cons

Disadvantage Details
⚠️ Runtime errors possible Misconfigurations often surface at runtime, not compile-time

🧠 Hidden logic

Can be hard to trace mappings in large teams or codebases

🚀 Performance cost Small, but noticeable when mapping thousands of objects or large graphs
🔍 Harder debugging Especially when maps are layered or custom resolvers are involved

Seeing about manual mapping below.

What is Manual Mapping?

Manual mapping means you explicitly write the code to copy data between two different object types, for example, from a Domain Model (your internal business object) to a DTO (used to transfer data across layers or over the network), or vice versa.

This is done without using any helper libraries like AutoMapper; you manually assign each property.

Why Map Between DTO and Domain Model?

Mapping isolates your internal models from external representation and often improves security and performance.

Manual Mapping Example in C#

Suppose you have these classes.

// Domain Model
public class User
{
    public int    Id           { get; set; }
    public string FullName     { get; set; }
    public string Email        { get; set; }
    public string PasswordHash { get; set; } // sensitive, do not expose
}
// DTO (e.g. for API response)
public class UserDto
{
    public int    Id       { get; set; }
    public string FullName { get; set; }
    public string Email    { get; set; }
}

Manual Mapping Code

Mapping Domain Model → DTO.

public UserDto MapToDto(User user)
{
    if (user == null)
        return null;
    return new UserDto
    {
        Id       = user.Id,
        FullName = user.FullName,
        Email    = user.Email
    };
}

Mapping DTO → Domain Model.

public User MapToDomain(UserDto dto)
{
    if (dto == null)
        return null;
    return new User
    {
        Id         = dto.Id,
        FullName   = dto.FullName,
        Email      = dto.Email,
        // PasswordHash left unchanged or set elsewhere
    };
}

Pros of Manual Mapping

Cons of Manual Mapping

Manual Mapping (Implicit/Explicit Conversion)

Example: Operator Overload

public class ProductDto
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public static explicit operator Product(ProductDto dto) =>
        new Product
        {
            Name  = dto.Name,
            Price = dto.Price
        };
}

Pros

Advantage Details
<🧪 Compile-time safety No hidden runtime mapping failures
🚀 Better performance No reflection or delegate caching; pure C#
🔍 Easier to debug You see the exact mapping logic
💡 Full control Perfect for custom mapping, validations, domain rules

Cons

Disadvantage Details
🧱 More boilerplate You write each mapping manually
👥 Less DRY Tedious with many DTOs
🔁 Can duplicate logic Especially when mapping many nested types

So, Which Should You Use?

Factor Use AutoMapper If… Use Manual Mapping If…
🔄 Entities are small-medium
🧱 Entities are large/complex ⚠️ Not ideal
🧪 You want compile-time safety
⚡ Performance-critical ⚠️ Use with care
👥 Team readability/debugging ⚠️ Not always clear
🧰 Rapid CRUD scaffolding ✅ Fast setup ❌ Slower
🛠️ You need full control

Recommendation

We will see more details in the next article. Thank you.