Is it possible to map multiple DTO objects to a single ViewModel using Automapper?

Check for following link regarding your query

http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx


You could create a composite DTO that holds two or more DTO objects and map the composite DTO to the output view model.


If you have 2 DTO classes and 1 flattened view model:

public class Dto1
{
    public string Property1 { get; set; }
}
public class Dto2
{
    public string Property2 { get; set; }
}
public class FlattenedViewModel
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

And you create mappings for both DTOs to view model:

CreateMap<Dto1, FlattenedViewModel>();
CreateMap<Dto2, FlattenedViewModel>();

You can map 1st DTO to the model and then just "append" 2nd DTO:

var dto1 = new Dto1 { Property1 = "Value1"; }
var dto2 = new Dto2 { Property2 = "Value2"; }

var model = Mapper.Map<FlattenedViewModel>(dto1); // map dto1 properties
Mapper.Map(dto2, model); // append dto2 properties