Automapper:如何映射嵌套对象? [英] Automapper: how to map nested object?

查看:328
本文介绍了Automapper:如何映射嵌套对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Automapper语法. 我有一个PropertySurveys列表,每个列表包含1个Property. 我希望将集合中的每个项目映射到一个结合了2个类的新对象中.

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.

所以我的代码如下;

            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);
            }

我的简化类如下;

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; }
}

因此在PropertyToSurveyOutput对象中,设置了第一个映射PropertyName之后.然后,在设置第二个映射PropertySurveyName之后,但是PropertyName被覆盖为null. 我该如何解决?

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?

推荐答案

首先,Automapper支持集合映射.您无需将每个项目都映射成一个循环.

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).

最后-使用Automapper,您可以创建映射并定义如何对某些属性进行自定义映射:

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));

用法:

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);

结果:

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


更新:如果您的Property类具有许多属性,则可以定义两个默认映射-一个来自Property的映射:


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

Mapper.CreateMap<Property, PropertyToSurveyOutput>();

PropertySurvey中的一个.在PropertySurvey中使用映射后,请使用第一个映射:

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));

这篇关于Automapper:如何映射嵌套对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆