I was wondering if it is possible to map multiple DTO objects to a single ViewModel object using Automapper?
Essentially, I have multiple DTO objects and would like to display information from each on a single screen in ASP.NET MVC 2.0. To do so I would like to flatten the DTO objects (or parts of them...) into the Viewmodel and pass said viewmodel to the view. If I had one DTO this would be easy, but I've never seen it being done with multiple. Obviously there are a number of roundabout ways to do this (outside of automapper), but this is the approach that I would like to take if possible.
Check for following link regarding your query
http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx
You can add a Map override extension method off IMappingEngine that takes a params array. Something like:
You could then call it like this:
You could create a composite DTO that holds two or more DTO objects and map the composite DTO to the output view model.
I just worked this out myself and have a great solution. It is most likely that your two views are actually related somehow in your system (especially if you are using Entity Framework). Check your models and you should see something which displays the relationship, if you don't then just add it. (The
virtual
)Your models
Your ViewModels
Automapper looks like this:
I'm assuming you are getting your data with LinQ:
Viola, everything will work. In your view just access by using
model.dto2.PropertyB
If you have 2 DTO classes and 1 flattened view model:
And you create mappings for both DTOs to view model:
You can map 1st DTO to the model and then just "append" 2nd DTO:
This is the information from the expired link in this answer: https://stackoverflow.com/a/8923063/2005596
When using AutoMapper (http://automapper.codeplex.com ) I often have a scenario where I need to map several entities into one entity. Commonly this occurs when mapping from a number domain entities into a single view model (ASP.NET MVC). Unfortunately the AutoMapper API does not expose functionality to map several entities into one entity; however it is relatively simple to create some helper method to do this. Below I will illustrate the approach that I have taken.
In this example I have the following entities in my domain model
In addition to this I have a requirement to render the person details the person’s address and any related comment on a single page (using ASP.NET MVC). To implement this I have created the view model shown below, which includes data from all three of the domain entities shown above
In the controller action method I make three separate calls into the domain layer to retrieve the required entities but this still leaves the issue that I need to map several source entities onto a single destination entity. To perform this mapping I created a helper class which encapsulates AutoMapper and exposes functionality which enables the mapping of several source objects onto one destination object. This class is shown below
To map several source objects onto one destination I have made use of the functionality provided by AutoMapper which allows you to perform a mapping between a source object and a destination object that already exists.
Finally below is the code from the controller that retrieves the three entities and performs the mapping onto a single view model