In this part I will be showing how Automapper uses flattening by convention.
If we look at our OrderDto we will see that it has a IEnumerable of OrderItemsDto and also something called OrderNumber. This is the type of object we want to send to our presentation layer.
1 2 3 4 5 6 7 |
public class OrderDto { public string CustomerName { get; set; } public decimal Total { get; set; } public string OrderNumber { get; set; } public IEnumerable<OrderItemsDto> LineItems { get; set; } } |
Compare that to our domain object where it has a GetTotal method, OrderNo and a Customer Object
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 |
//Domain Object 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; } } //Customer Domain Object public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Bio { get; set; } public string GetName() { return FirstName + ' ' + LastName; } } |
In Automapper there is a convention where it sees the method has a Get in front it. It will then auto flatten it by convention. See how in the OrderDto I have Total as a property and also CustomerName.
Our mapping code would look something like this where we are mapping OrderNo to OrderNumber
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 27 28 29 |
public ActionResult Index() { var orders = _respository.GetAll(); AutoMapper.Mapper.CreateMap<Order, OrderDto>() .ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo)) .ForMember(dest => dest.OrderItemsDto, opt => opt.Ignore()); var model = AutoMapper.Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders); return View(model); } //Razor View Code <h2>Orders</h2> <table> <tr> <th>Order Number</th> <th>Name</th> <th>Total</th> </tr> @foreach (var item in Model) { <tr> <td>@item.OrderNumber</td> <td>@item.CustomerName</td> <td>$@item.Total</td> </tr> } </table> |
With this mapping will not get the inner mapping of OrderItems since we have told Automapper to Ignore those guys don’t map the OrderItems, as it stands OrderItemsDto would be null
This would produce a view looking like
As you can see Automapper automatically has pulled out GetTotal and populated Total Property and so is Customer object with GetName method with CustomerName. By using these simple conventions it automatically does the work for you without explicitly specifying it.
In next part we will talk about Nested mapping.
Previous Post:
Part 1 Null Subsitution
[…] and also something called OrderNumber. This is the type of object we want to send to our… [full post] taswar Taswar Bhatti Blog .netautomapper 0 0 0 0 0 […]
Thank you so much for this nice tutorial.
It is really helpful for me.
I am waiting for the next.
Thanks a lot