简体   繁体   中英

AutoMapper.Map ignore all Null value properties from source object

I'm trying to map 2 objects of the same type. What I want to do is AutoMapper to igonore all the properties, that have Null value in the source object, and keep the existing value in the destination object.

I've tried using this in my "Repository", but it doesn't seem to work.

Mapper.CreateMap<TEntity, TEntity>().ForAllMembers(p => p.Condition(c => !c.IsSourceValueNull));

What might be the problem ?

Interesting, but your original attempt should be the way to go. Below test is green:

using AutoMapper;
using NUnit.Framework;

namespace Tests.UI
{
    [TestFixture]
    class AutomapperTests
    {

      public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int? Foo { get; set; }
        }

        [Test]
        public void TestNullIgnore()
        {
            Mapper.CreateMap<Person, Person>()
                    .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

            var sourcePerson = new Person
            {
                FirstName = "Bill",
                LastName = "Gates",
                Foo = null
            };
            var destinationPerson = new Person
            {
                FirstName = "",
                LastName = "",
                Foo = 1
            };
            Mapper.Map(sourcePerson, destinationPerson);

            Assert.That(destinationPerson,Is.Not.Null);
            Assert.That(destinationPerson.Foo,Is.EqualTo(1));
        }
    }
}

Condition overload with 3 parameters let's you make expression equivalent to your example p.Condition(c => !c.IsSourceValueNull) :

Method signature:

void Condition(Func<TSource, TDestination, TMember, bool> condition

Equivalent expression:

CreateMap<TSource, TDestination>.ForAllMembers(
    opt => opt.Condition((src, dest, sourceMember) => sourceMember != null));

So far I've solved it like this.

foreach (var propertyName in entity.GetType().GetProperties().Where(p=>!p.PropertyType.IsGenericType).Select(p=>p.Name))
   {
      var value = entity.GetType().GetProperty(propertyName).GetValue(entity, null);
      if (value != null)
      oldEntry.GetType().GetProperty(propertyName).SetValue(oldEntry, value, null);
    }

but still hoping to find a solution using AutoMapper

Taking Marty's solution (which works) one step further, here is a generic method to make it easier to use:

public static U MapValidValues<U, T>(T source, U destination)
{
    // Go through all fields of source, if a value is not null, overwrite value on destination field.
    foreach (var propertyName in source.GetType().GetProperties().Where(p => !p.PropertyType.IsGenericType).Select(p => p.Name))
    {
        var value = source.GetType().GetProperty(propertyName).GetValue(source, null);
        if (value != null && (value.GetType() != typeof(DateTime) || (value.GetType() == typeof(DateTime) && (DateTime)value != DateTime.MinValue)))
        {
            destination.GetType().GetProperty(propertyName).SetValue(destination, value, null);
        }
    }

    return destination;
}

Usage:

class Person
{
    public string Name { get; set; } 
    public int? Age { get; set; } 
    public string Role { get; set; } 
}

Person person = new Person() { Name = "John", Age = 21, Role = "Employee" };
Person updatePerson = new Person() { Role = "Manager" };

person = MapValidValues(updatePerson, person);

Work Around - Add DataMember property in destination type [DataMember(EmitDefaultValue = false)] will remove the property if the source value is null.

[DataContract]
class Person
{
    [DataMember]
    public string Name { get; set; } 

    [DataMember]
    public int? Age { get; set; } 

    [DataMember(EmitDefaultValue = false)]
    public string Role { get; set; } 
}

if the Role value is null Role property will be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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