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,
productwill be a new instance with the same data fromdto.
Compatible type conversion
MapTo<T>() also handles automatic type conversions, such as:
- From
stringtoenum - From
enumtostring
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 toOrderStatus.Confirmed.
Notes
- Only public properties with both
getandsetare 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.