X

Automapper: Mapping objects Part 2 of 7 (Flattening by Convention)

automapper

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.

 public class OrderDto
    {
        public string CustomerName { get; set; }
        public decimal Total { get; set; }
        public string OrderNumber { get; set; }
        public IEnumerable LineItems { get; set; }
    }

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

//Domain Object
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; }
    }

//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

 public ActionResult Index()
        {
            var orders = _respository.GetAll();

            AutoMapper.Mapper.CreateMap()
                .ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo))
                .ForMember(dest => dest.OrderItemsDto, opt => opt.Ignore());

            var model = AutoMapper.Mapper.Map, IEnumerable>(orders);

            return View(model);
        }
//Razor View Code

Orders

@foreach (var item in Model) { }
Order Number Name Total
@item.OrderNumber @item.CustomerName $@item.Total

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

Categories: .NET Automapper
Taswar Bhatti:

View Comments (1)

  • Thank you so much for this nice tutorial.
    It is really helpful for me.
    I am waiting for the next.
    Thanks a lot

Related Post