3

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

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.

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?

ferpega
  • 3,182
  • 7
  • 45
  • 65
  • 1
    ```cfg.CreateMap().ReverseMap().ForMember(d=>d.Nicks, o=> { o.MapFrom(s=>s.Nicks); o.UseDestinationValue(); }); ``` See [this](https://github.com/AutoMapper/AutoMapper/issues/2126) for details. – Lucian Bargaoanu Oct 31 '17 at 14:54
  • Hi @LucianBargaoanu the key is your `o.UseDestinationValue();` Thanks. Could you write it as an answer to accept it? Regards. – ferpega Oct 31 '17 at 15:26
  • Nevermind. But feel free to write a more detailed answer if you want. – Lucian Bargaoanu Oct 31 '17 at 15:27
  • @LucianBargaoanu I had a question. Is there a way do define this globally. I was updating my Mapper and it seems that the private setters are not getting mapped after v6. I have private setter all over the project and it seems difficult to individually set this. – pratikvasa Jul 09 '20 at 05:42
  • https://docs.automapper.org/en/latest/10.0-Upgrade-Guide.html#all-collections-are-mapped-by-default-even-if-they-have-no-setter – Lucian Bargaoanu Jul 09 '20 at 06:15

1 Answers1

4

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

Regards.

ferpega
  • 3,182
  • 7
  • 45
  • 65