5

I'm having some problems working out how to get Automapper 4.2.1 to allow for a type mapping where the destination value might be null depending on the source value.

Older versions of Automapper allowed an AllowNullDestination flag to be set via the Mapper configuration but I can't find the equivalent recipe for the new version and the old mechanism of configuring via the static Mapper object seems to have been obsoleted.

I have tried the following without success:

  • Mapper.Configuration.AllowNullDestinationValues = true;
  • Mapper.AllowNullDestinationValues = true;
  • Mapper.Initialize(c=>c.AllowNullDestinationValues=true);

Here's a simple test case demonstrating the problem. This fails on the final line with an AutoMapperMappingException since the Substitute method is returning null. I would like both mappings to succeed.

I would prefer to avoid the use of .ForMember in the solution since in the real scenario I'm trying to address, the mapping between bool and 'object' (actually a custom class) should apply across the entire object tree.

Although there are several similar questions here on StackOverflow, I haven't found one that refers to a recent version of Automapper.

Thanks in advance for any suggestions

using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AutoMapperTest
{
    [TestClass]
    public class ExampleTest
    {
        [TestMethod]
        public void NullDestinationCanBeMapped()
        {
            var mapper = new MapperConfiguration(configuration =>
            {
                configuration.CreateMap<Source, Target>();
                //How should the following mapping be modified to pass the test?   
                configuration.CreateMap<bool, object>()
                .Substitute(i => i ? null : new object());
            }).CreateMapper();

            var target1 = mapper.Map<Source, Target>(new Source {Member = false}); //succeeds
            Assert.IsNotNull(target1.Member); //pass
            var target2 = mapper.Map<Source, Target>(new Source {Member = true}); //fails to map with exception
            Assert.IsNull(target2.Member); //not reached
        }
    }

    public class Source
    {
        public bool Member { get; set; }
    }

    public class Target
    {
        public object Member { get; set; }
    }
}
2
  • 1
    have u tried Mapper.Configuration.AllowNullDestinationValues = true ? Commented Apr 1, 2016 at 8:35
  • 1
    This is marked as obsolete in V4 and doesn't work. I have tried all of the following without success... . * Mapper.Configuration.AllowNullDestinationValues = true; * Mapper.AllowNullDestinationValues = true; Mapper.Initialize(c=>c.AllowNullDestinationValues=true); Commented Apr 1, 2016 at 8:56

1 Answer 1

0

Instead of using Substitute, use ConvertUsing...

     configuration.CreateMap<bool, MyClass>()
     .ConvertUsing(i => i ? null : new object());

Not the answer you're looking for? Browse other questions tagged or ask your own question.