title: AutoMapper Map properties of nested objects if Not Null
body: <p>I have the following sample objects..</p>
  
  <pre><code>  public class ComplexObject
    {
      public string Name { get; set; }
      public SimpleObject Child1 { get; set; }
      public SimpleObject Child2 { get; set; }
    }
    public class SimpleObject : IEquatable&lt; SimpleObject &gt;
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Gender { get; set; }
      public int? Age { get; set; }
    }
  </code></pre>
  
  <p>with the following AutoMapper configuration</p>
  
  <pre><code>  Mapper.CreateMap&lt;SimpleObject, SimpleObject&gt;()
      .ForAllMembers(expression=&gt;expression.Condition(r=&gt;!r.IsSourceValueNull));
  
    Mapper.CreateMap&lt;ComplexObject, ComplexObject&gt;()
      .ForAllMembers(expression=&gt;expression.Condition(resolutionContext=&gt;!resolutionContext.IsSourceValueNull));
  </code></pre>
  
  <p>and the following NUnit test...</p>
  
  <pre><code>[SetUp] public void Should_run_before_each_test()
  {
    child1 = new SimpleObject { FirstName = "Tom", LastName = "Smith", Age = 34, Gender = "Male" };
    child2 = new SimpleObject { FirstName = "Andy", LastName = "Smith-bob", Age = 21, Gender = "Male" };
  }
  
  [ Test ]
  public void Should_ignore_null_properties_in_nested_objects()
  {
    var source = new ComplexObject()
    {
      Name = "blue",
      Child1 = new SimpleObject{FirstName = "dot", LastName = "net"}
    };
    var destination = new ComplexObject()
    {
      Name = "Andy",
      Child1 = child1,
      Child2 = child2
    };
  
    destination = Mapper.Map(source, destination);
  
    destination.Name.Should(Be.EqualTo(source.Name));
    destination.Child1.FirstName.Should(Be.EqualTo("dot"));
    destination.Child1.LastName.Should(Be.EqualTo("net")  );
    destination.Child1.Age.Should(Be.EqualTo(child1.Age)  );
    destination.Child1.Gender.Should(Be.EqualTo(child1.Gender)  );
  }
  </code></pre>
  
  <p>The above test fails when asserting the age as AutoMapper is pushing null through to the destination object.</p>
  
  <p>Am I expecting too much from AutoMapper, or have I missed some vital map configuration step.</p>
  
  <p>The ultimate goal is to have a very complex domain object bound to incoming form data via an MVC action. AutoMapper will then be used to merge only non-null properties (at all depths of the object graph) into the real instance being maintained throughout a multi step form.</p>
  
  <p>Just in case anyone needs to know... I have also tried the following mapping configuration without any luck :(</p>
  
  <pre><code>  Mapper.CreateMap&lt;ComplexObject, ComplexObject&gt;()
      .ForMember(x=&gt;x.Child1, l=&gt;l.ResolveUsing(x=&gt;x.Child1 == null?null:Mapper.Map&lt;SimpleObject,SimpleObject&gt;(x.Child1)))
      .ForMember(x=&gt;x.Child2, l=&gt;l.ResolveUsing(x=&gt;x.Child2 == null?null:Mapper.Map&lt;SimpleObject,SimpleObject&gt;(x.Child2)))
      .ForAllMembers(expression=&gt;expression.Condition(resolutionContext=&gt;!resolutionContext.IsSourceValueNull));
  </code></pre>