AutoMapper does not convert empty lists well

0

There is a problem with the AutoMapper, when mapping an empty list, it is entrusted to a list with Count = 0, regardless of whether the configuration is specified that the values are not taken into account = null

link

code:

using System;
using System.Collections.Generic;
using AutoMapper;

public class Program
    {
       public static void Main(string[] args)
        {
            Mapper.Initialize(cfg =>
            {
                cfg.AllowNullCollections = true;
                cfg.CreateMap<GeneralInfo, GeneralInfo>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) =>
                    srcMember != null
                ));

            });
            GeneralInfo gi = new GeneralInfo();
            gi.Descr = "Test";
            gi.Dt = DateTime.Now;
            gi.Qty = 1;
            gi.PersonList = new List<Person>();
            gi.PersonList.Add(new Person { Num = 1, Name = "John", Surname = "Scott" });

            GeneralInfo gi2 = new GeneralInfo();
            gi2.Qty = 3;

            Console.WriteLine("Count antes de mapeo = " + gi.PersonList.Count);

            Mapper.Map<GeneralInfo, GeneralInfo>(gi2, gi);

            Console.WriteLine("Count despues de mapeo = " + gi.PersonList.Count);
            // Error : gi.PersonList.Count == 0 !!!! 
            //por que? si arriba esta: Condition((src, dest, srcMember) => srcMember != null ...

        }
    }

    class Person
    {
        public int Num { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    class GeneralInfo
    {
        public int? Qty { get; set; }
        public DateTime? Dt { get; set; }
        public string Descr { get; set; }
        public List<Person> PersonList { get; set; }
    }
    
asked by Алексей Владышевский 15.12.2017 в 12:08
source

1 answer

0

This happens because your second object does not also have a child object, then when you trace one over the other one loses the property

internal class Program
{
    private static void Main(string[] args)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AllowNullCollections = true;
            cfg.CreateMap<GeneralInfo, GeneralInfo>()
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) =>
                    srcMember != null
                ));
        });
        var gi = new GeneralInfo
        {
            Descr = "Test",
            Dt = DateTime.Now,
            Qty = 1,
            PersonList = new List<Person> { new Person { Num = 1, Name = "John", Surname = "Scott" } }
        };

        var gi2 = new GeneralInfo
        {
            Qty = 3,
            PersonList = new List<Person> { new Person { Num = 1, Name = "Thiago", Surname = "Loureiro" } }
        };

        Console.WriteLine("Count antes de mapeo = " + gi.PersonList.Count);

        Mapper.Map(gi2, gi);

        Console.WriteLine("Count despues de mapeo = " + gi.PersonList.Count);
        // Error : gi.PersonList.Count == 0 !!!!
        //por que? si arriba esta: Condition((src, dest, srcMember) => srcMember !=         }
    }
}

internal class Person
{
    public int Num { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
}

internal class GeneralInfo
{
    public int? Qty { get; set; }
    public DateTime? Dt { get; set; }
    public string Descr { get; set; }
    public List<Person> PersonList { get; set; }
}
    
answered by 15.12.2017 в 15:15