Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 215
JP. Avatar asked Jan 24 '10 16:01

JP.


People also ask

Can AutoMapper map collections?

Polymorphic element types in collectionsAutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.

What is AutoMapper good for?

AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.

How do I use AutoMapper to list a map?

Once you have your types, and a reference to AutoMapper, you can create a map for the two types. Mapper. CreateMap<Order, OrderDto>(); The type on the left is the source type, and the type on the right is the destination type.

When should I use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.


2 Answers

Check for following link regarding your query

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

like image 55
Bahdeng Avatar answered Sep 29 '22 18:09

Bahdeng


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

like image 21
Alexander Groß Avatar answered Sep 29 '22 18:09

Alexander Groß