0

我有以下类,我需要将代理映射到 AgentDto。

public class ObjectsAddedToCollectionProperties : Dictionary<string, ObjectList> {}

public class OriginalValuesDictionary : Dictionary<string, Object> {}

public class ObjectChangeTracker()
{
  public ObjectsAddedToCollectionProperties ObjectsAddedToCollectionProperties { get; set; }
  public OriginalValuesDictionary OriginalValues { get; set; }
}

public class BaseDto
{
  public ObjectChangeTracker ChangeTracker { get; set; }
}

public class RolesDto : BaseDto {}

public class AgentDto : BaseDto 
{
   ICollection<RoleDto> Roles { get; set; }
}

public class ModelBase
{
  public ObjectChangeTracker ChangeTracker { get; set; }
}

public class Role : ModelBase {}

public class Agent : ModelBase 
{
   ICollection<Role> Roles { get; set; }
}

这是我的映射器配置

Mapper.CreateMap<Agent, AgentDto>().IgnoreAllNonExisitng();
Mapper.CreateMap<Role, RoleDto>().IgnoreAllNonExisitng();

var agent = new Agent();
Mapper.Map<AgentDto>(agent);

当我将 Agent 映射到 AgentDto 时,角色已正确映射,但我添加到其中一个字典的任何内容都未映射。我认为这是因为 AutoMapper 无法告诉目标类型应该是什么,并且因为目标类型是 Object,它推断它可以简单地复制源对象。

我创建了一个自定义解析器,但由于某种原因它没有被调用

class ChangeTrackerResolver : ValueResolver<ObjectChangeTracker, ObjectChangeTracker>
    {
        protected override ObjectChangeTracker ResolveCore(ObjectChangeTracker source)
        {
            var typeMap = Mapper.GetAllTypeMaps();

            var destination = new ObjectChangeTracker();

            foreach (var keyValuePairs in source.ObjectsAddedToCollectionProperties)
            {
                var objectList = new ObjectList();
                objectList.AddRange(from value in keyValuePairs.Value
                                    let sourceType = value.GetType()
                                    let destinationType = typeMap.Where(map => map.SourceType == sourceType).Single().DestinationType
                                    select Mapper.Map(value, sourceType, destinationType));

                destination.ObjectsAddedToCollectionProperties.Add(keyValuePairs.Key, objectList);
            }

            foreach (var keyValuePairs in source.ObjectsRemovedFromCollectionProperties)
            {
                var objectList = new ObjectList();
                objectList.AddRange(from value in keyValuePairs.Value
                                    let sourceType = value.GetType()
                                    let destinationType = typeMap.Where(map => map.SourceType == sourceType).Single().DestinationType
                                    select Mapper.Map(value, sourceType, destinationType));

                destination.ObjectsRemovedFromCollectionProperties.Add(keyValuePairs.Key, objectList);
            }

            foreach (var keyValuePairs in source.OriginalValues)
            {
                var sourceType = keyValuePairs.Value.GetType();
                var destinationType = typeMap.Where(map => map.SourceType == sourceType).Single().DestinationType;
                destination.OriginalValues.Add(keyValuePairs.Key, Mapper.Map(keyValuePairs.Value, sourceType, destinationType));
            }

            foreach (var keyValuePairs in source.ExtendedProperties)
            {
                var sourceType = keyValuePairs.Value.GetType();
                var destinationType = typeMap.Where(map => map.SourceType == sourceType).Single().DestinationType;
                destination.ExtendedProperties.Add(keyValuePairs.Key, Mapper.Map(keyValuePairs.Value, sourceType, destinationType));
            }

            return destination;
        }
    }

这是我的映射

Mapper.CreateMap<ModelBase, BaseDto>()
                .Include<Agent, AgentDto>()
                .Include<Address, AddressDto>()
                .Include<BankAccount, BankAccountDto>()
                .Include<Client, ClientDto>()
                .Include<Person, PersonDto>()
                .Include<Role, RoleDto>()
                .Include<TelephoneNumber, TelephoneNumberDto>()
                .Include<Height, HeightDto>()
                .Include<Weight, WeightDto>()
                .IgnoreAllNonExisitng().ForMember(
                a => a.ChangeTracker,
                opt => opt.ResolveUsing<ChangeTrackerResolver>());

编辑

我已经解决了我的映射问题,现在我的解析器被调用,但是字典中的对象,例如 ObjectsAddedToCollectionProperties,在传递给解析器的 ChangeTracker 中不存在。这是我的新映射。

Mapper.CreateMap<Agent, AgentDto>()
                .IgnoreAllNonExisitng().ForMember(
                a => a.ChangeTracker,
                opt => opt.ResolveUsing<ChangeTrackerResolver>().FromMember(a => a.ChangeTracker));
4

0 回答 0