0

I tried to use AutoMapper NullSubstitute feature with source member and destination member of type string.

It doesn't seem to work with Projection.

As an example, I adapted src/UnitTests/Projection/NullSubstitutes.cs.

namespace AutoMapper.UnitTests.Projection
{
    using System.Collections.Generic;
    using System.Linq;
    using QueryableExtensions;

    using Xunit;

    public class NullSubstitutesWithMapFrom
    {
        private List<Dest> _dests;

        public class Source
        {
            public string Value { get; set; }
        }

        public class Dest
        {
            public string ValuePropertyNotMatching { get; set; }
        }

        [Fact]
        public void Can_substitute_null_values()
        {
            MapperConfiguration Configuration = new MapperConfiguration(cfg =>
          {
              cfg.CreateMap<Source, Dest>().ForMember(m => m.ValuePropertyNotMatching, opt =>
              {
                  opt.MapFrom(src => src.Value);
                  opt.NullSubstitute("5");
              });

          });

            var source = new[] { new Source { Value = null } }.AsQueryable();
            _dests = source.ProjectTo<Dest>(Configuration).ToList();

            Assert.Equal("5", _dests[0].ValuePropertyNotMatching);
        }
    }
}

Expected : "5" equals "5" Actual : "5" equals null

With Map everything is ok

namespace AutoMapper.UnitTests.Projection
{
    using System.Collections.Generic;
    using System.Linq;

    using Xunit;

    public class NullSubstitutesWithMapFrom
    {
        private List<Dest> _dests;

        public class Source
        {
            public string Value { get; set; }
        }

        public class Dest
        {
            public string ValuePropertyNotMatching { get; set; }
        }

        [Fact]
        public void Can_substitute_null_values()
        {
            MapperConfiguration Configuration = new MapperConfiguration(cfg =>
          {
              cfg.CreateMap<Source, Dest>().ForMember(m => m.ValuePropertyNotMatching, opt =>
              {
                  opt.MapFrom(src => src.Value);
                  opt.NullSubstitute("5");
              });

          });

            var source = new[] { new Source { Value = null } }.ToList();
            _dests = Configuration.CreateMapper().Map<List<Dest>>(source);
            Assert.Equal("5", _dests[0].ValuePropertyNotMatching);


        }
    }
}

It looks a bit like what was described in https://github.com/AutoMapper/AutoMapper/issues/642

  • 1
    Move the null check inside the `MapFrom`. I'm not sure why, but for `ProjectTo`, the `NullSubstitute` only applies for `Nullable`. – Lucian Bargaoanu Jun 12 '20 at 09:22
  • Thanks for the advice @LucianBargaoanu. I already did it actually and it works. But accordiing to some digging, it should work. I've seen a couple of examples with objets. – Alcated Jun 12 '20 at 09:33
  • Let's say it's an entity type, not string. What would you put as null substitute? Try that in a EF linq query, without AM. – Lucian Bargaoanu Jun 12 '20 at 10:26
  • I get your point and it's true that all the examples that I've seen are using Map and not ProjectTo. e.g : - https://weblogs.asp.net/psteele/automapper-handling-null-members - https://dotnettutorials.net/lesson/usevalue-resolveusing-and-null-substitution-using-automapper/ Do you know if it has always been like that for ProjecTo, Nullsubstitute and string ? Have you heard about a fix planned for some day? – Alcated Jun 12 '20 at 16:47
  • https://github.com/AutoMapper/AutoMapper/pull/3438/commits/26293ad5b3d3568b15cfddd210eb5b335ffbd98a – Lucian Bargaoanu Jun 12 '20 at 16:55
  • Thank you. I subscribed to the Merge Request. – Alcated Jun 15 '20 at 13:41

0 Answers0