AutoMapper自定义解析器不起作用

时间:2014-03-07 05:30:49

标签: c# asp.net list mapping automapper

我使用AutoMapper来映射不同命名空间中的类的对象(即不同的类库)。两个类都有相同的结构。他们包含一些其他复杂类的成员。下面是我为映射编写的代码。

//Structure of ActivePrticipant class

public class ActiveParticipant
{
  //Few properties with basic data types
  public List<CodedId> Roles
  {
    get{return _roles;}
    set
            {
                if (value == null)
                {
                    _roles = new List<CodedId>();
                }
                else
                {
                    _roles = value;
                }
             }
   }
}

CodedId是另一个类。

首先,我尝试使用下面的代码进行映射,因为名称相同

Mapper.CreateMap<ActiveParticipant, SomeOtherNameSpace.ActiveParticipant>();
Mapper.AssertConfigurationIsValid();

但它在映射角色时抛出异常并建议使用自定义映射或自定义解析器。所以我使用了以下代码的自定义解析器。

IEnumerable<ActiveParticipant> activeParticipants = SomeMethod();
Mapper.CreateMap<ActiveParticipant, SomeOtherNameSpace.ActiveParticipant>().ForMember(dest => dest.Roles, map => map.ResolveUsing<GetCodedID>().FromMember(source => source.Roles)).ForMember(dest1 => dest1.AccessPoint, map1 => map1.ResolveUsing<getAccessPointIdentification>().FromMember(source1 => source1.AccessPoint));
Mapper.AssertConfigurationIsValid();
IEnumerable<SomeOtherNameSpace.ActiveParticipant> AP = null;
AP = Mapper.Map<IEnumerable<ActiveParticipant>, IEnumerable<SomeOtherNameSpace.ActiveParticipant>>(activeParticipants,AP);

//Custom resolvers
public class GetCodedID : ValueResolver<IEnumerable<CodedId>,  IEnumerable<SomeOtherNameSpace.CodedId>>
{
    protected override IEnumerable<SomeOtherNameSpace.CodedId> ResolveCore(IEnumerable<CodedId> source)
    {
        System.Collections.Generic.List<SomeOtherNameSpace.CodedId> roles = null;

        foreach (var item in source)
        {
            SomeOtherNameSpace.CodedId codedid = new SomeOtherNameSpace.CodedId
            {
                Property1 = item.Property1,
                Property2 = item.Property2,
                Property3 = item.Property3
            };
            roles.Add(codedid);
        }
        return roles;
    }

}

public class getAccessPointIdentification : ValueResolver<AccessPointIdentification, SomeOtherNameSpace.AccessPointIdentification>
{
    protected override SomeOtherNameSpace.AccessPointIdentification ResolveCore(AccessPointIdentification source)
    {
        SomeOtherNameSpace.AccessPointIdentification accessPoints = new SomeOtherNameSpace.AccessPointIdentification(source.Id, (SomeOtherNameSpace.AccessPointType)source.AccessPointType);

        return accessPoints;
    }

}

但仍然会抛出异常

Missing type map configuration or unsupported mapping.
Mapping types:
CodedId -> CodedId
CodedId -> SomeOtherNameSpace.CodedId

Destination path:
IEnumerable`1[0][0]

Source value:
CodedId{ Property1:test, Property2:test2, Property3:Test3}

此外,我无法调试自定义解析器代码。 如何映射这两个对象?

1 个答案:

答案 0 :(得分:1)

您已在以下位置之间定义了映射:

Mapper.CreateMap<ActiveParticipant, SomeOtherNameSpace.ActiveParticipant>();

您还需要:

Mapper.CreateMap<CodedId, SomeOtherNameSpace.CodedId>();