Collection of zero emptying collection sources

I am combining objects that have a List element. I tell AutoMapper to ignore elements with source code, but when I merge into an object with a zero set, the receiver gets an empty collection (even if it has elements in front of the map).

Any ideas on how to prevent this?

        ConfigurationInfo template1 = new ConfigurationInfo() {
            Columns = null //1st templates list of columns is null
        };

        ConfigurationInfo template2 = new ConfigurationInfo() {
            Columns = new List<ColumnInfo>()
        };
        template2.Columns.AddRange(existingColumns); //template2.Columns.Count == 9

        ConfigurationInfo template3 = new ConfigurationInfo() {
            Columns = null //3rd templates list of columns is null
        };

        var config = new AutoMapper.MapperConfiguration(cfg => {
            cfg.AllowNullCollections = true;
            cfg.AllowNullDestinationValues = true;

            cfg.CreateMap<ConfigurationInfo, ConfigurationInfo>()
                .ForAllMembers(option => {
                    //explicitly telling automapper to ignore null source members...
                    option.Condition((source, destination, sourceMember, destMember) => sourceMember != null);
                });

        });

        var mapper = config.CreateMapper();

        ConfigurationInfo finalTemplate = new ConfigurationInfo();

        mapper.Map(template1, finalTemplate); 
        //finalTemplate.Columns == null, which is exptected
        mapper.Map(template2, finalTemplate); 
        //finalTemplate.Columns.Count == 9, still exptected
        mapper.Map(template3, finalTemplate); 
        //finalTemplate.Columns.Count == 0, expecting 9 b/c 3rd template.Columns == null so AutoMapper should ignore. why is this happening?
+5
source share
4 answers

I struggled with the same. There are a couple of configurations that should solve this problem, but they do not work in my case. Perhaps they will work for you.

In this case, it is in the global configuration.

 Mapper.Initialize(cfg =>
{
      cfg.AllowNullCollections = true;
      cfg.AllowNullDestinationValues = true;
}

Here is a function request that I created in my repo. They offer some work that can help you.

https://github.com/AutoMapper/AutoMapper/issues/2341

, , , . null default. :

public static bool IsNullOrDefault(this object obj)
{
    return obj == null || (obj.GetType() is var type && type.IsValueType  && obj.Equals(Activator.CreateInstance(type)));
}
+1

IEnumerable.

public class IgnoringNullValuesTypeConverter<T> : ITypeConverter<T, T> where T : class
{
    public T Convert(T source, T destination, ResolutionContext context)
    {
        return source ?? destination;
    }
}

cfg.CreateMap<IEnumerable, IEnumerable>().ConvertUsing(new IgnoringNullValuesTypeConverter<IEnumerable>());

, .

0

AllowNullCollections , AllowNullCollections Profile, :

public class MyMapper : Profile
{
    public MyMapper()
    {
        // Null collections will be mapped to null collections.
        AllowNullCollections = true;

        CreateMap<MySource, MyDestination>();
    }
}
0

null ExpandoObject, expando .

        var patchTemplate = new ExpandoObject() as IDictionary<string, object>;

        foreach (var property in updatedTemplateDetails.GetType().GetProperties())
            if (property.GetValue(updatedTemplateDetails) is var propertyValue && propertyValue != null )
                patchTemplate.Add(property.Name, propertyValue);

        Mapper.Map(patchTemplate, finalTemplate);
-1
source

Source: https://habr.com/ru/post/1673357/


All Articles