AutoMapper-如何在映射中使用集合索引



我想将Dto映射到实体,并将Dto的数组索引存储到实体的属性中。

例如(伪(:

class Dto {}
class Entity{ int Index; }
// perform mapping from collection of Dto, to collection of Entity
Map<Entity>(new Dto[]{ new Dto(), new Dto(), new Dto() };
// maps to
Entity[]{
Entity{ Index = 0 },
Entity{ Index = 1 },
Entity{ Index = 2 }
}

当然我可以手动完成,但是,我的体系结构中已经有了AutoMapper,所以为它配置映射是有意义的

我找不到做这件事的方法。我找到了这个线索:https://github.com/AutoMapper/AutoMapper/issues/1238

这暗示了使用解析程序,但是,解析程序不再能够访问解析层次结构/堆栈。我相信这可能是由于性能原因而更改的,因为上面的评论是写的。

在我看来,我应该能够很容易地访问"根"源对象(传递给Map<>(源(方法的对象/集合(,而拥有这样的访问权限将使解决这个问题变得微不足道,但我找不到它。

有指针吗?

谢谢,

以下是如何使用AfterMap:实现它

Mapper.CreateMap<SourceDto, Destination>()
.AfterMap((_, dest) => 
{
int id = 0;
foreach (var entity in dest.Entities)
{
entity.Index = id++;
}
});

最新更新