简体   繁体   English

AutoMapper非破坏性列表对帐?

[英]AutoMapper non-destructive list reconciliation?

I have a list on the target object that I want to reconcile against the source. 我有一个要与源协调的目标对象的列表。

The list is already created, I just want AutoMapper to add/update/delete items from this list, at no time should the list be set to a new object reference. 该列表已经创建,我只想让AutoMapper从该列表中添加/更新/删除项目,任何时候都不应将该列表设置为新的对象引用。 How can I do this? 我怎样才能做到这一点?

The reason for this is that the target object is an ORM generated code, and for whatever reason it has no setter. 这样做的原因是目标对象是ORM生成的代码,无论出于何种原因,它都没有设置器。

The target looks like this: 目标看起来像这样:

 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(); 
 }

From my experience, AutoMapper isn't really designed with mapping onto already populated objects in mind. 根据我的经验,AutoMapper并不是真正针对映射到已经填充的对象而设计的。 But one way to do what you're asking is to configure AutoMapper to ignore the EntityList property for mapping, then use AfterMap to call some custom reconciliation function you write to align the two lists: 但是,您要执行的操作的一种方法是将AutoMapper配置为忽略EntityList属性进行映射,然后使用AfterMap调用您编写的一些自定义对帐函数来对齐两个列表:

Mapper.CreateMap<Source, Target>()
              .ForMember(dest => dest.EntityList, opt => opt.Ignore())
              .AfterMap((src, dest) => Reconcile(src.EntityList, dest.EntityList));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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