Null 替換(Null Substitution)
Null
替換允許當(dāng)源類型成員在成員鏈任何位置為Null
時給目標(biāo)成員提供一個備用的值。這意味著目標(biāo)成員不再映射為Null
而是你提供的備用值斤讥。
var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()
.ForMember(destination => destination.Value, opt => opt.NullSubstitute("Other Value")));
var source = new Source { Value = null };
var mapper = config.CreateMapper();
var dest = mapper.Map<Source, Dest>(source);
dest.Value.ShouldEqual("Other Value");
source.Value = "Not null";
dest = mapper.Map<Source, Dest>(source);
dest.Value.ShouldEqual("Not null");
在目標(biāo)類型之后任何映射/轉(zhuǎn)換都是以源成員類型來進(jìn)行的纱皆。