Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping collections using AutoMapper

Tags:

c#

automapper

I'm trying to map an array into an ICollection of type <T>.

Basically I want to be able to do:

Mapper.CreateMap<X[], Y>(); 

Where Y is Collection<T>

Any ideas?

like image 940
Brian Liang Avatar asked Oct 26 '09 10:10

Brian Liang


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.

How do I use AutoMapper to list a map?

How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.

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.


1 Answers

You don't need to setup your mapping for collections, just the element types. So just:

Mapper.CreateMap<X, Y>(); Mapper.Map<X[], Collection<Y>>(objectToMap); 

See here for more info: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home

like image 111
Drew Freyling Avatar answered Sep 19 '22 03:09

Drew Freyling