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.

Compare that to our domain object where it has a GetTotal method, OrderNo and a Customer Object

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

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