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
//Order
public class Order
{
public string OrderNo { get; set; }
public Customer Customer { get; set; }
public DateTime PurchaseDate { get; set; }
public IEnumerable 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
public class OrderDto
{
public string CustomerName { get; set; }
public decimal Total { get; set; }
public string OrderNumber { get; set; }
public IEnumerable 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 to OrderDto we will also provide the mapping of OrderItems to OrderItemsDto.
public ActionResult OrderItems()
{
var orders = _respository.GetAll();
Mapper.CreateMap()
.ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo));
Mapper.CreateMap();
var model = Mapper.Map, IEnumerable>(orders);
return View(model);
}
//View
Order Number | Name | Total | Items |
---|---|---|---|
@item.OrderNumber | @item.CustomerName | $@item.Total |
@foreach (var child in item.LineItems)
{
@string.Format("({0}) {1} - ${2}", @child.Quantity, @child.Name, @child.Price) } |
As you can see I have taught automapper about its inner child/object mapping before calling Map, thus it knows about the inner mapping, our result would show like
In the next part I will be showing projection in Automapper.
Previous Post:
Part 1 NullSubsitution
Part 2 Flattening by Convention
View Comments (10)
Have you used an sql profiler to see the queries sent to the database? The code might look clean but might hide some unoptimized sql queries, that will not be good on production.
Valid point but in my demo I am not using sql, my repository is just a bunch of objects that are manually created. I do use sqlserver in another project and have not found a big performance issue since we have a dba who is optimizing all the time, and in my last part of this series I will be showing some tips on performance with Automapper.
Mapper.CreateMap()
.ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo));
How I can map property from nullable nested object?
In situation when orderItem.Order == null a had Object reference not set to an instance of an object exception.
One can use the NullSubsitute in my first posting I showed how to use a NullSubsitute of it. Or you could consider Ignore so that it doesnt map.
http://taswar.zeytinsoft.com/2011/03/07/automapper-mapping-objects-part-1-of-7-nullsubsitution/
Do you still use Automapper or there is a better alternative to this? I am having challenges mapping multiple sources into a single destination.
I dont use Automapper anymore since I dont do much .NET development these days.
But here are some alternatives.
EmitMapper, http://emitmapper.codeplex.com/
ValueInjecter https://github.com/omuleanu/ValueInjecter
BLToolkit https://github.com/igor-tkachev/bltoolkit
This doesn't seem to work if there is only 1 parent and many children. For example...If you were only bringing back a single Order with multiple Order Items.
This
var model = Mapper.Map<IEnumerable, IEnumerable>(orders);
would turn into this
var model = Mapper.Map(orders);
I think without the IEnumerable on the parent, it is not loading the child list.
My code samples got messed up when I hit Post. I hope you get the gist of what I am saying. I will try it again.
var model = Mapper.Map<IEnumerable, IEnumerable>(orders);
var model = Mapper.Map(order);
Just wondering can you not go with Mapper.Map < Order, IEnum > .
I found the solution for this in https://dotnetsolutionsbyvenkat.blogspot.com/2020/07/how-to-map-properties-of-one-class-to.html.
Nice artical with good explanation