我有一个目标对象的列表,我希望与源协调。
列表已经创建,我只希望AutoMapper在此列表中添加/更新/删除项目,列表不应设置为新的对象引用。我怎么能这样做?
原因是目标对象是ORM生成的代码,无论出于何种原因它都没有setter。
目标如下:
public class Target
{
//This is the target member.
public List EntityList
{
get{
return _entityList;
}
}
//Except this is a much more complicated, track-able list
private List _entityList= new List();
}
答案 0 :(得分:0)
根据我的经验,AutoMapper实际上并没有考虑到已经填充的对象的映射。但是,您要问的一种方法是将AutoMapper配置为忽略EntityList
属性进行映射,然后使用AfterMap
调用您编写的一些自定义协调功能以对齐两个列表:
Mapper.CreateMap<Source, Target>()
.ForMember(dest => dest.EntityList, opt => opt.Ignore())
.AfterMap((src, dest) => Reconcile(src.EntityList, dest.EntityList));