Base solution for your next web application
Open Closed

One Source - multiple destination - CustomDtoMapper - null #5079


User avatar
0
OriAssurant created

I am trying to map 2 complex object in CustomDtoMapper and unable to do it because the second mapping, does not seem to work.

Second Mapping to EntityLine from DtoInputEntity results in null values. none of the values get assigned.

Here is my CustomDtoMapper class:

internal static class CustomDtoMapper { private static volatile bool _mappedBefore; private static readonly object SyncObj = new object();

    public static void CreateMappings(IMapperConfigurationExpression mapper)
    {
        lock (SyncObj)
        {
            if (_mappedBefore)
            {
                return;
            }

            CreateMappingsInternal(mapper);              

            _mappedBefore = true;
        }
    }
          

    private static void CreateMappingsInternal(IMapperConfigurationExpression mapper)
    {
        mapper.CreateMap<User, UserEditDto>()
            .ForMember(dto => dto.Password, options => options.Ignore())
            .ReverseMap()
            .ForMember(user => user.Password, options => options.Ignore());
   
        
        mapper.CreateMap<DtoInputEntity, Entity>()
              .ForMember(entityCreateDto => entityCreateDto.OrderNumber,
                        dtoInputEntity => dtoInputEntity.MapFrom(input => input.Response.OrderNumber))
              .ForMember(entityCreateDto => entityCreateDto.ShipmentNumber,
                        dtoInputEntity => dtoInputEntity.MapFrom(input => input.Response.ShipmentNumber));
            
        mapper.CreateMap<DtoInputEntity, EntityLine>()
           .ForPath(entityLineCreateDto => entityLineCreateDto.ExternalModelIdentifier, opt => opt.MapFrom(dtoInputEntity => dtoInputEntity.Response.UTModelIdentifier))
           .ForPath(entityLineCreateDto => entityLineCreateDto.SKU, opt => opt.MapFrom(dtoInputEntity => dtoInputEntity.Response.ModelIdentifier))
           .ForPath(entityLineCreateDto => entityLineCreateDto.ShipmentNumber, opt => opt.MapFrom(dtoInputEntity => dtoInputEntity.Response.ShipmentNumber))
           .ForPath(entityLineCreateDto => entityLineCreateDto.LineNumber, opt => opt.MapFrom(dtoInputEntity => dtoInputEntity.Response.LineNumber));
        
    }
}

3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    @OriAssurant, sorry for our late response. Can you also share your mapping code ?

  • User Avatar
    0
    OriAssurant created

    This is how I intatiate the mapping call:

    public string ManageOrder(DtoInputEntity inputEntity) { //this works Entity dto = new Entity(); dto = inputEntity.MapTo<Entity >();

            //this does not
             EntityLine dtoLine = new EntityLine();
              dtoLine = inputEntity.MapTo&lt;EntityLine &gt;();
    

    }

    is it because Shipment Number is being assigned against both Entity and EntityLine?

  • User Avatar
    0
    ismcagdas created
    Support Team

    is it because Shipment Number is being assigned against both Entity and EntityLine?

    Does it work when you comment out below line in mapping configuration ?

    .ForPath(entityLineCreateDto => entityLineCreateDto.ShipmentNumber, opt => opt.MapFrom(dtoInputEntity => dtoInputEntity.Response.ShipmentNumber))