简体   繁体   English

无嵌入器的Automapper嵌套集合

[英]Automapper nested Collections without setter

I have this code snippet running on LinqPad (C# program) with Automapper Nuget package 6.1.1 already included: 我在LinqPad(C#程序)上运行了这个代码片段,其中包含Automapper Nuget包6.1.1:

void Main()
{
    Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Top, TopDto>().ReverseMap();
            });

    Mapper.AssertConfigurationIsValid();

    var source = new TopDto
    {
        Id = 1,
        Name = "Charlie",
        Nicks = new List<string> { "Fernandez", "Others" }
    };


    var destination = Mapper.Map<Top>(source);

    destination.Dump();

}

// Define other methods and classes here
public class Top
{
    public Top()
    {
        Nicks = new List<string>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<string> Nicks { get; }
}

public class TopDto
{
    public TopDto()
    {
        Nicks = new List<string>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<string> Nicks { get; set; }
}

And as you can see we have problems setting the nested Collection (without Setter at all). 正如您所看到的,我们在设置嵌套Collection时遇到了问题(根本没有Setter)。 In theory this should be running fine but it is not adding any element to the Collection. 从理论上讲,这应该运行正常,但它不会向Collection中添加任何元素。

If we change the collection property adding a public setter, then all is fine. 如果我们更改集合属性添加公共setter,那么一切都很好。

How can I get a nested collection without adding a public setter or a setter at all? 如何在不添加公共setter或setter的情况下获得嵌套集合?

Thanks to @LucianBargaoanu (in the comments) this is solved now, in this way: 感谢@LucianBargaoanu(在评论中)现在以这种方式解决了这个问题:

void Main()
{
    Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Top, TopDto>().ReverseMap()
                    .ForMember(d => d.Nicks, o=> 
                                                { 
                                                    o.MapFrom(s => s.Nicks);
                                                    o.UseDestinationValue(); 
                                                });
            });

    Mapper.AssertConfigurationIsValid();

    var source = new TopDto(new List<string> { "Fernandez", "Others" })
    {
        Id = 1,
        Name = "Charlie"
    };


    var destination = Mapper.Map<Top>(source);

    destination.Dump();

}

// Define other methods and classes here
public class Top
{
    public Top()
    {
        Nicks = new List<string>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<string> Nicks { get; }
}

public class TopDto
{
    public TopDto(List<string> nicks)
    {
        Nicks = nicks;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<string> Nicks { get; private set; }
}

Regards. 问候。

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

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