简体   繁体   中英

Define global BeforeMap and AfterMap callbacks for AutoMapper

I know about the feature of defining a Before/AfterMap callback on the map level for a given type pair. However, I'm searching for a solution to define a global Before/AfterMap function somehow, which would apply to every defined type map.

In most of my DTOs I have a mechanism which prevents changed notifications temporarly with the BeginUpdate/EndUpdate pattern. I would like AutoMapper to wrap the mapping between these calls whenever the target type supports it.

I've looked through questions here and the AutoMapper docs but haven't found a native solution.

I think I've found a porposed solution, but haven't tested it completely yet.

After all of my maps are registered, I would do something like this:

var typeMaps = Mapper.GetAllTypeMaps();
foreach (var typeMap in typeMaps)
{
    typeMap.AddBeforeMapAction(...);
    typeMap.AddAfterMapAction(...);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM