13.AutoMapper of mapping before and after (Before and After Map Action)

https://www.jianshu.com/p/1ff732094f21

Mapping before and after (Before and After Map Action)

You may occasionally need to perform custom logic before and after the mapping occurs. This should be rare, in this operation AutoMapperthan more reasonable. But you still can use before / after mapping action to achieve their goals:

Mapper.Initialize(cfg => {
  cfg.CreateMap<Source, Dest>()
    .BeforeMap((src, dest) => src.Value = src.Value + 10) .AfterMap((src, dest) => dest.Name = "John"); }); 

Or create before / after mapping callback when mapping:

int i = 10;
Mapper.Map<Source, Dest>(src, opt => {
    opt.BeforeMap((src, dest) => src.Value = src.Value + i);
    opt.AfterMap((src, dest) => dest.Name = HttpContext.Current.Identity.Name); }); 

The latter is useful when the context information before and after the mapping operation associated with the need to use configuration.

Guess you like

Origin www.cnblogs.com/zengpeng/p/11059945.html