Flattening

One of the common usages of object-object mapping is to take a complex object model and flatten it to a simpler model. You can take a complex model such as:

public class Order
{
	private readonly IList<OrderLineItem> _orderLineItems = new List<OrderLineItem>();

	public Customer Customer { get; set; }

	public OrderLineItem[] GetOrderLineItems()
	{
		return _orderLineItems.ToArray();
	}

	public void AddOrderLineItem(Product product, int quantity)
	{
		_orderLineItems.Add(new OrderLineItem(product, quantity));
	}

	public decimal GetTotal()
	{
		return _orderLineItems.Sum(li => li.GetTotal());
	}
}

public class Product
{
	public decimal Price { get; set; }
	public string Name { get; set; }
}

public class OrderLineItem
{
	public OrderLineItem(Product product, int quantity)
	{
		Product = product;
		Quantity = quantity;
	}

	public Product Product { get; private set; }
	public int Quantity { get; private set;}

	public decimal GetTotal()
	{
		return Quantity*Product.Price;
	}
}

public class Customer
{
	public string Name { get; set; }
}

We want to flatten this complex Order object into a simpler OrderDto that contains only the data needed for a certain scenario:

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

When you configure a source/destination type pair in AutoMapper, the configurator attempts to match properties and methods on the source type to properties on the destination type. If for any property on the destination type a property, method, or a method prefixed with "Get" does not exist on the source type, AutoMapper splits the destination member name into individual words (by PascalCase conventions).

// Complex model

var customer = new Customer
	{
		Name = "George Costanza"
	};
var order = new Order
	{
		Customer = customer
	};
var bosco = new Product
	{
		Name = "Bosco",
		Price = 4.99m
	};
order.AddOrderLineItem(bosco, 15);

// Configure AutoMapper

Mapper.CreateMap<Order, OrderDto>();

// Perform mapping

OrderDto dto = Mapper.Map<Order, OrderDto>(order);

dto.CustomerName.ShouldEqual("George Costanza");
dto.Total.ShouldEqual(74.85m);

We configured the type map in AutoMapper with the CreateMap method. AutoMapper can only map type pairs it knows about, so we have explicitly register the source/destination type pair with CreateMap. To perform the mapping, we use the Map method.

On the OrderDto type, the Total property matched to the GetTotal() method on Order. The CustomerName property matched to the Customer.Name property on Order. As long as we name our destination properties appropriately, we do not need to configure individual property matching.

Last edited Feb 1, 2009 at 8:29 PM by jbogard, version 2

Comments

MrChriZ May 19, 2013 at 12:07 PM 
One problem I'm seeing is that say your customer domain object had the property CustomerName instead of just Name then the DTO needs to have the property
CustomerCustomerName

It would be good if Automapper could check the property isn't prefixed already with the class name.

jbogard Feb 18, 2010 at 5:13 PM 
@kbaltrinic

We thought about this, but the problem of naming collisions just came up too much for our applications. We might have a HomeAddress and a BusinessAddress, so we want that distinction. It gets worse with something like an "ID" property, where it could be on many, many types in the graph.

kbaltrinic Feb 17, 2010 at 11:23 AM 
AutoMapper is working really well for us in general. Kudos to Jimmy.

It would be great if we had some control over the conventions used in flattening. For example, a lot of our dto's don't prefix their property names because it doesn't make much sense. In terms of the example above, our "order" dto would not have a "CustomerName" property, just a "Name" property.

An example from our domain is the User type which has an Address property of type PostalAddress with properties like StreetAddress, City, State, etc. The UserDto likewise has the properties StreetAddress, City, State, etc., not AddressStreetAddress, AddressCity, etc for obvious reasons. An easy way to flatten this w/o having to use the projection syntax would save a lot of boilerplate .ForMember(..., opt => opt.MapFrom(...)) calls.

I am not sure if this is the best syntax but something like .ForSource( domainObj => domainOjb.Address).DestinationPrefix("") would b e nice.