Skip to content

Understanding Mapping with MapTo()

The MapTo<T>() method is the core feature of the NetCore.Mapper library.

It allows you to transform a source object into a new instance of a target type based on simple, automatic rules — no manual mapping configuration required.


How it works

The transformation occurs by matching public property names and compatible types.

For example:

public class ProductDto
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}
var dto = new ProductDto { Name = "T-Shirt", Price = 49.90m };
var product = dto.MapTo<Product>();

In this case, product will be a new instance with the same data from dto.


Compatible type conversion

MapTo<T>() also handles automatic type conversions, such as:

  • From string to enum
  • From enum to string
public class OrderDto
{
    public string Status { get; set; } // example: "Confirmed"
}

public enum OrderStatus
{
    Pending,
    Confirmed,
    Shipped
}

public class Order
{
    public OrderStatus Status { get; set; }
}
var dto = new OrderDto { Status = "Confirmed" };
var order = dto.MapTo<Order>();

The string "Confirmed" will be automatically converted to OrderStatus.Confirmed.


Notes

  • Only public properties with both get and set are considered
  • The target type must have a public parameterless constructor
  • Nested complex types are not mapped recursively (e.g., objects inside objects)

Simple, direct, and functional

With MapTo<T>(), you avoid manual mapping configuration and focus on what really matters:
safely and fluently transforming data between layers of your application.