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