In this part we learn about Nested Mapping in Automapper and we will use the same OrderDto Object that we had previously but we will let Automapper to map the inner objects. Once again here is the Domain Objects First
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Order public class Order { public string OrderNo { get; set; } public Customer Customer { get; set; } public DateTime PurchaseDate { get; set; } public IEnumerable<OrderItems> LineItems { get; set; } public bool ShipToHomeAddress { get; set; } public decimal GetTotal() { return LineItems == null ? 0 : LineItems.Sum(x => x.GetTotalPrice()); } public Guid InternalId { get; set; } } //OrderItems public class OrderItems { public decimal Price { get; set; } public string Name { get; set; } public int Quantity { get; set; } public decimal GetTotalPrice() { return Price * Quantity; } } |
The Dto (Data Transfer Object) look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class OrderDto { public string CustomerName { get; set; } public decimal Total { get; set; } public string OrderNumber { get; set; } public IEnumerable<OrderItemsDto> LineItems { get; set; } } //OrderItemsDto public class OrderItemsDto { public string Name { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } } |
This time when we map Order […]