13.AutoMapper 之映射先後(Before and After Map Action)

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

映射先後(Before and After Map Action)

你可能偶爾須要在映射發生先後執行自定義邏輯。這應該不多見,這種操做放在AutoMapper以外更加合理。不過你仍是能夠使用before/after 映射動做來達到目的:javascript

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

或者在映射時建立before/after 映射回調:java

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); }); 

後一種配置在映射動做先後須要用到關聯的上下文信息時頗有用。app

相關文章
相關標籤/搜索