With Automapper we can automatically perform deep copies between different object through reflection. The default mapping attempt to copy properties with the same name from the source type to the destination type. Is very useful when we want to generate to copy a model object to a view model object or to a subclassed object.

To perform a copy we have to map each involved source/destination type for our copy. In this case we are creating a mapping between the GlobalData and the SpecificData types to get a new SpecificData instance from a GlobalData instance.

Mapper.CreateMap<GlobalData, SpecificData>();
SpecificData dst = Mapper.Map<GlobalData, SpecificData>(src);

Using a custom Constructor

We can define as well more complex mappings. In this case we are instructing Automapper to use a particular constructor to create our MyFeedItem instances. As we can see from this example we can map a collection just by mapping its inner element’s type.

Mapper.CreateMap<OuterFeed, MyFeedItem>()
    .ConstructUsing((OuterFeed src) => new MyFeedItem(GlobalData));
var Feeds = Mapper.Map<IEnumerable<OuterFeed>, IEnumerable<MyFeedItem>>(feeds);

Mapping through Subclasses

If we need to map a type which includes some property of abstract/interface type we have to tell Automapper how to map each of its derived/concrete types. We can have the same situation even when we are working with subclassed object.

In this example we want to map a ItemModel type to a ItemViewModel type. The ItemModel type includes a property of type RelatedItemModel, that is an abstract class, so within an instance we’ll find one of its derived types: RelatedItemModelCountry, RelatedItemModelGender or RelatedItemModelGeneric.

On the other side, the ItemViewModel type includes a correspondent property of type RelatedItemViewModel, that is an abstract class as well, so within an instance we’ll find one of its derived types: RelatedItemViewModelCountry, RelatedItemViewModelGender or RelatedItemViewModelGeneric.

AutoMapper.Mapper.CreateMap<ItemModel, ItemViewModel>();
AutoMapper.Mapper.CreateMap<RelatedItemModel, RelatedItemViewModel>()
    .Include<RelatedItemModelCountry, RelatedItemViewModelCountry>()
    .Include<RelatedItemModelGender, RelatedItemViewModelGender>()
    .Include<RelatedItemModelGeneric, RelatedItemViewModelGeneric>();
AutoMapper.Mapper.CreateMap<RelatedItemModelCountry, RelatedItemViewModelCountry>();
AutoMapper.Mapper.CreateMap<RelatedItemModelGender, RelatedItemViewModelGender>();
AutoMapper.Mapper.CreateMap<RelatedItemModelGeneric, RelatedItemViewModelGeneric>();
var itemViewModel= AutoMapper.Mapper.Map<ItemModel, ItemViewModel>(item);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s