简体   繁体   English

如何在不使用内联映射或多个.ForMember 的情况下在自动映射器中嵌套 map

[英]How to map nested object in automapper without using inline mapping or multiple .ForMember?

There is a question , that describes what i want to get very precisely, but they are using inline mapping.有一个问题,它非常准确地描述了我想要得到的东西,但他们使用的是内联映射。

Source/destination types源/目标类型

public class SrcInner
{
    public int A {get;set;} // imagine here 100500 properties
}

public class SrcOuter
{
    public int B {get;set;}
    public SrcInner C {get;set}
}

public class Dest
{
    public int A {get;set;} // so here imagine 100500 same properties, as in SrcInner
    public int B {get;set;}
}

Mapping configuration映射配置

public static void AddMapper(this IServiceCollection services)
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<SrcInner, Dest>();
        cfg.CreateMap<SrcOuter, Dest>();
    });

    var mapper = config.CreateMapper();

    services.AddSingleton(mapper);
}

Expected behavior预期行为

Both A and B properties are filled after mapping.映射后填充 A 和 B 属性。

Actual behavior实际行为

Only B property is filled after mapping.映射后仅填充 B 属性。

Steps to reproduce重现步骤

public class Foo
{
    IMapper Mapper{get;set;}
    public Foo(IMapper mapper) // comes through dependency injection
    {
        Mapper = mapper;
    }

    public Bar()
    {
        var test = new SrcOuter()
        {
            B = 10;
            C = new SrcInner()
            {
                A = 10;
            }
        }
        var testDest = new Dest();
        mapper.Map(test, Dest);
    }
}

Is there a proper way to set configuration that way, so such mapping will work?有没有合适的方法来设置配置,所以这样的映射会起作用?


UPDATE更新

Is there a way to map Dest to SrcOuter with filling SrcInner?有没有办法通过填充 SrcInner 来 map Dest 到 SrcOuter?

The documentation describes how this can be achieved using IncludeMembers here该文档描述了如何在此处使用IncludeMembers来实现这一点

In your case, the config would be:在您的情况下,配置将是:

cfg.CreateMap<SrcOuter, Dest>().IncludeMembers(s => s.C);
cfg.CreateMap<SrcInner, Dest>(MemberList.None);

If you want this to work both ways, the most idiomatic way to do this is to prefix your destination members with the nested source object name, so that flattening and unflattening works automatically.如果您希望这双向工作,最惯用的方法是使用嵌套源 object 名称作为目标成员的前缀,以便展平和非展平自动工作。

Eg if you have the following objects:例如,如果您有以下对象:

public class SrcInner
{
    public int A { get; set; }
}

public class SrcOuter
{
    public int B { get; set; }
    public SrcInner Inner { get; set; }
}

public class Dest
{
    public int InnerA { get; set; }
    public int B { get; set; }
}

You don't need any more configuration than:您不需要更多的配置:

cfg.CreateMap<SrcOuter, Dest>().ReverseMap();

I think you need to look at this: https://docs.automapper.org/en/stable/Projection.html我想你需要看看这个: https://docs.automapper.org/en/stable/Projection.html

And then you need to然后你需要

 .ForMember(dest => dest.A, opt => opt.MapFrom(src => src.C.A))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM