AutoMapper - 映射System.Object的复杂列表和字典

时间:2013-08-25 18:39:00

标签: automapper

我有以下类,我需要将Agent映射到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时,Roles被正确映射,但是我添加到其中一个Dictionaries的任何内容都没有映射。我假设这是因为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>());

修改

我已经修复了我的映射问题,我的Resolver现在被调用了,但是Dictionaries中的对象,例如ObjectsAddedToCollectionProperties在传递给Resolver的ChangeTracker中不存在。这是我的新映射。

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

0 个答案:

没有答案