Automapper: how to map nested object?

时光怂恿深爱的人放手 提交于 2019-12-23 10:05:04

问题


I am struggling with the Automapper syntax. I have a List of PropertySurveys, each containing 1 Property. I wish to map each item on the collection into a new object which combines the 2 classes.

So my code looks like;

            var propertySurveys = new List<PropertyToSurveyOutput >();
            foreach (var item in items)
            {
                Mapper.CreateMap<Property, PropertyToSurveyOutput >();
                var property = Mapper.Map<PropertyToSurvey>(item.Property);
                Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput >();
                property = Mapper.Map<PropertyToSurvey>(item);
                propertySurveys.Add(property);
            }

My simplified classes look like;

public class Property
{
    public string PropertyName { get; set; }
}

public class PropertySurvey
{
    public string PropertySurveyName { get; set; }
    public Property Property { get; set;}
}

public class PropertyToSurveyOutput
{
    public string PropertyName { get; set; }
    public string PropertySurveyName { get; set; }
}

So in the PropertyToSurveyOutput object, after the first mapping PropertyName is set. Then after the second mapping PropertySurveyName is set, but PropertyName is overridden to null. How do I fix this?


回答1:


First of all, Automapper supports mapping of collections. You don't need to map each item in a loop.

Second - you don't need to re-create map each time you need to map single object. Put mappings creation to application start code (or before first usage of mapping).

And last - with Automapper you can create mapping and define how to do custom map for some properties:

Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput>()
   .ForMember(pts => pts.PropertyName, opt => opt.MapFrom(ps => ps.Property.PropertyName));

Usage:

var items = new List<PropertySurvey>
{
    new PropertySurvey { 
          PropertySurveyName = "Foo", 
          Property = new Property { PropertyName = "X" } },
    new PropertySurvey { 
          PropertySurveyName = "Bar", 
          Property = new Property { PropertyName = "Y" } }
};

var propertySurveys = Mapper.Map<List<PropertyToSurveyOutput>>(items);

Result:

[
  {
    "PropertyName": "X",
    "PropertySurveyName": "Foo"
  },
  {
    "PropertyName": "Y",
    "PropertySurveyName": "Bar"
  }
]

UPDATE: If your Property class has many properties, you can define two default mappings - one from Property:

Mapper.CreateMap<Property, PropertyToSurveyOutput>();

And one from PropertySurvey. And use first mapping after you used mapping from PropertySurvey:

Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput>()
      .AfterMap((ps, pst) => Mapper.Map(ps.Property, pst));



回答2:


First rule of automapper property name should be same then only it will map correctly and assign the value but in your case one property name is only "Property" and second property name is "PropertyName" so make property name same it will work for you



来源:https://stackoverflow.com/questions/34271334/automapper-how-to-map-nested-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!