0

I have question about Automapper. This is my usecase: I have these classes:

Bussiness:

public class Item
{
    public NestedItem NestedItem{get;set;}
}

public class NestedItem
{
    public string Name{get;set;}
}

ViewModel:

public class ItemViewModel
{
    public string NestedItemName{get;set;}
}

When I want to map Item to ItemViewModel. Automapper map property NestedItem.Name to NestedItemName correctly. But in the opposite way it doesn´t work. Mapping doesn´t change value of NestedItem.Name.

Marek
  • 372
  • 5
  • 15
  • possible duplicate of [Automapper map into nested class](http://stackoverflow.com/questions/7924185/automapper-map-into-nested-class) – Andrew Whitaker Nov 06 '14 at 02:30

1 Answers1

0

I've got a slightly contradictory answer to the link stated above, maybe I just go about my things the awkward way but I believe you can do it within one map. For example:

Mapper.CreateMap<ItemViewModel, Item>()
    .ForMember(dest => dest.NestedItem, opt => 
         opt.MapFrom(src => new NestedItem(src.NestedItemName));

Now of course this would require you to create a new constructor or add a blank constructor and use curly brackets.

I can't comment on performance as AutoMapper is a seriously complicated monster. However this should do the trick.

Heberda
  • 830
  • 7
  • 29