Question

I am using AutoMapper to map objects of classes which are in different namespace(i.e. different class library). Both classes have same structure. They contains members of some othe complex class. Below is the code I have written for mapping.

//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 is another class.

First I tried to map using below code as names are identical

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

but it thrown exception in mapping Roles and suggested to use custom mapping or custom resolver. So I used custom resolver with below code.

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

}

But still it throws below exception

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}

Moreover I am not able to debug custome resolver code. How can I map both of these objects?

Était-ce utile?

La solution

You have defined a mapping between:

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

You also need:

Mapper.CreateMap<CodedId, SomeOtherNameSpace.CodedId>();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top