AutoMapper非破坏性列表对帐?

时间:2013-07-26 00:28:28

标签: c# data-structures orm automapper automapper-2

我有一个目标对象的列表,我希望与源协调。

列表已经创建,我只希望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(); 
 }

1 个答案:

答案 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));