Leman

Occasionally, you might need to perform custom logic before or after a map occurs. These should be a rarity, as it\'s more obvious to do this work outside of AutoMapper. You can create global before/after map actions:

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

Or you can create before/after map callbacks during 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 configuration is helpful when you need contextual information fed into before/after map actions.

分类:

技术点:

相关文章:

  • 2021-05-07
  • 2021-10-25
  • 2021-05-10
  • 2021-08-05
  • 2021-12-16
  • 2021-12-20
猜你喜欢
  • 2021-12-19
  • 2021-11-16
  • 2021-10-29
  • 2021-07-14
  • 2021-07-03
  • 2021-11-28
相关资源
相似解决方案