Marty
Marty

Reputation: 3555

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 ?

Upvotes: 28

Views: 32242

Answers (6)

Ali Tayyip Aydin
Ali Tayyip Aydin

Reputation: 43

I used this code and it's worked

.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

Upvotes: 0

Nenad
Nenad

Reputation: 26647

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

Upvotes: 15

Srinath Subramani
Srinath Subramani

Reputation: 29

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.

Upvotes: 0

primaryobjects
primaryobjects

Reputation: 55

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

Upvotes: 0

Void Ray
Void Ray

Reputation: 10209

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

Upvotes: 32

Marty
Marty

Reputation: 3555

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

Upvotes: 1

Related Questions