空类型映射

Null substitution allows you to supply an alternate value for a destination member if the source value is null anywhere along the member chain. This means that instead mapping from null, it will map from the value you supply.

var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Dest>()
    .ForMember(dest => dest.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");
posted @ 2016-08-15 21:07  柠檬头  阅读(85)  评论(0编辑  收藏  举报