【发布时间】:2020-02-19 01:05:28
【问题描述】:
我将域模型映射到 DTO,反之亦然。我正在尝试将我的 API 配置为接受带有集合的 DTO,其中该集合的顺序将映射到我的域对象中的 int Sequence 以实现持久性。
public class Model {
public ICollection<Fields> Fields { get; set; }
}
public class Field {
public int Sequence { get; set; }
}
CreateMap<ModelView, Model>()
.ForMember(x => x.Fields, opt => opt...)
// here I want to specify that currentField.Sequence = Model.Fields.IndexOf(currentField)
// , or to set it equal to some counter++;
;
这样的事情在 Automapper 中是可能的,还是我必须编写自己的 ConstructUsing() 方法来执行此逻辑?我对使用 ConstructUsing() 犹豫不决,因为我为 Field DTO 指定了一个映射,我不想重复该逻辑。
我还希望能够对其进行配置,以便当我回到我的 DTO (Model -> ModelView) 时,我可以按照指定的顺序将 Fields 插入到集合中Sequence.
【问题讨论】:
标签: c# automapper