Automapper嵌套没有setter的Collections [英] Automapper nested Collections without setter

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

问题描述

我已经在具有Automapper Nuget软件包6.1.1的LinqPad(C#程序)上运行了以下代码段:

I have this code snippet running on LinqPad (C# program) with Automapper Nuget package 6.1.1 already included:

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

如您所见,我们在设置嵌套Collection时遇到了问题(根本没有Setter).从理论上讲,这应该可以正常运行,但是不会向Collection中添加任何元素.

And as you can see we have problems setting the nested Collection (without Setter at all). 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.

如何在不添加公共设置器或完全没有设置器的情况下获取嵌套集合?

How can I get a nested collection without adding a public setter or a setter at all?

推荐答案

感谢@LucianBargaoanu(在注释中),现在可以通过以下方式解决此问题:

Thanks to @LucianBargaoanu (in the comments) this is solved now, in this way:

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

致谢.

这篇关于Automapper嵌套没有setter的Collections的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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