Aggiornamento 1-13-10 Sono riuscito a trovare un po 'di successo utilizzando il seguente codice per la mappatura. In sostanza sto ignorando le proprietà che non hanno una mappatura e le mappano in seguito. Gradirei il feedback sul fatto che io vada o meno al riguardo nel modo migliore possibile. Inoltre, non sono sicuro su come procedere per testare questa mappatura. Ho avuto l'impressione che l'utilizzo di AutoMapper dovrebbe contribuire ad alleviare la noiosità del controllo di ogni proprietà.Eccezione mappatura personalizzata automapper
Ecco il mio nuovo codice:
Mapper.CreateMap<MoveEntity, MoveEntityDto>()
.ForMember(dest => dest.PrimaryOriginTransferee, opt => opt.Ignore())
.ForMember(dest => dest.PrimaryDestinationTransferee, opt => opt.Ignore())
.ForMember(dest => dest.Customer, opt => opt.Ignore())
.ForMember(dest => dest.DestinationAddress, opt => opt.Ignore())
.ForMember(dest => dest.OriginAddress, opt => opt.Ignore())
.ForMember(dest => dest.Order, opt => opt.Ignore())
.ForMember(dest => dest.Shipment, opt => opt.Ignore())
.ForMember(dest => dest.SourceSystemName, opt => opt.Ignore());
Mapper.CreateMap<ContactEntity, TransfereeEntityDto>();
Mapper.CreateMap<CustomerEntity, CustomerEntityDto>();
Mapper.CreateMap<AddressEntity, AddressEntityDto>();
Mapper.CreateMap<OrderEntity, OrderEntityDto>()
.ForMember(dest => dest.OrderForwarding, opt => opt.Ignore())
.ForMember(dest => dest.Forwarder, opt => opt.Ignore());
Mapper.CreateMap<ShipmentEntity, ShipmentEntityDto>()
.ForMember(dest => dest.Services, opt => opt.Ignore());
Mapper.CreateMap<ServiceEntity, ServiceEntityDto>()
.ForMember(dest => dest.ServiceTypeCode, opt => opt.Ignore()) //TODO: ServiceTypeCode not being mapped, should it?
.ForMember(dest => dest.SourceSystemName, opt => opt.MapFrom(src => Enum.GetName(typeof(SourceSystemName), src.SourceSystemName)));
Mapper.CreateMap<OrderForwardingEntity, OrderForwardingEntityDto>();
Mapper.AssertConfigurationIsValid();
MoveEntityDto moveEntityDto = Mapper.Map<MoveEntity, MoveEntityDto>(moveEntity);
moveEntityDto.PrimaryDestinationTransferee = Mapper.Map<ContactEntity, TransfereeEntityDto>(moveEntity.PrimaryDestinationTransferee);
moveEntityDto.PrimaryOriginTransferee = Mapper.Map<ContactEntity, TransfereeEntityDto>(moveEntity.PrimaryOriginTransferee);
moveEntityDto.Customer = Mapper.Map<CustomerEntity, CustomerEntityDto>(moveEntity.Customer);
moveEntityDto.DestinationAddress = Mapper.Map<AddressEntity, AddressEntityDto>(moveEntity.DestinationAddress);
moveEntityDto.OriginAddress = Mapper.Map<AddressEntity, AddressEntityDto>(moveEntity.OriginAddress);
moveEntityDto.Order = Mapper.Map<OrderEntity, OrderEntityDto>(moveEntity.Order);
moveEntityDto.Order.OrderForwarding = Mapper.Map<OrderForwardingEntity, OrderForwardingEntityDto>(moveEntity.Order.OrderForwarding);
//moveEntityDto.Order.Forwarder = Mapper.Map<ForwarderEntity, ForwarderEntityDto>(moveEntity.Order.Forwarder); //Apparently there is no forwarder entity for an Order
moveEntityDto.Shipment = Mapper.Map<ShipmentEntity, ShipmentEntityDto>(moveEntity.Shipment);
moveEntityDto.Shipment.Services = Mapper.Map<ServiceEntity[], ServiceEntityDto[]>(moveEntity.Shipment.ServiceEntities);
originale del messaggio:
Sto tentando di usare automapper per la prima volta al fine di mappare da un oggetto Bussiness ad un DTO. Sono in esecuzione in problemi che non so come risolvere, tra cui la seguente eccezione:
AutoMapper.AutoMapperMappingException: Cercando di mappare Graebel.SP.BO.MoveEntity a Graebel.SOA.Contracts.DataContracts.SP. MoveEntity. eccezione di tipo 'AutoMapper.AutoMapperMappingException' stato gettato
Ecco il codice automapper che io sono in esecuzione:
public MoveEntityDto MapMoveEntityToMoveEntityDto(MoveEntity moveEntity)
{
Mapper.CreateMap<MoveEntity, MoveEntityDto>()
.ForMember(dest => dest.PrimaryOriginTransferee, opt => opt.MapFrom(src => src.PrimaryOriginTransferee))
.ForMember(dest => dest.PrimaryDestinationTransferee,opt => opt.MapFrom(src => src.PrimaryDestinationTransferee))
.ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src.Customer))
.ForMember(dest => dest.DestinationAddress, opt => opt.MapFrom(src => src.DestinationAddress))
.ForMember(dest => dest.Order, opt => opt.MapFrom(src => src.Order))
.ForMember(dest => dest.OriginAddress, opt => opt.MapFrom(src => src.OriginAddress))
.ForMember(dest => dest.Shipment, opt => opt.MapFrom(src => src.Shipment))
.ForMember(dest => dest.SourceSystemName, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
MoveEntityDto moveEntityDto = Mapper.Map<MoveEntity, MoveEntityDto>(moveEntity);
return moveEntityDto;
}
Ecco la DTO (MoveEntityDto) che sto tentando di mappare:
public class MoveEntityDto
{
public bool IsOrderDetailPageModified { get; set; }
public bool IsRoutingPageModified { get; set; }
public bool IsServicePageModified { get; set; }
public bool IsContentAndContainerPageModified { get; set; }
public string FamilyRange { get; set; }
public string Office { get; set; }
public string ActivityType { get; set; }
public string ActivitySubject { get; set; }
public string ActivityNote { get; set; }
public TransfereeEntity PrimaryOriginTransferee { get; set; }
public TransfereeEntity PrimaryDestinationTransferee { get; set; }
public CustomerEntity Customer { get; set; }
public AddressEntity OriginAddress { get; set; }
public AddressEntity DestinationAddress { get; set; }
public OrderEntity Order { get; set; }
public ShipmentEntity Shipment { get; set; }
public string PortalId { get; set; }
public string SourceSystemId { get; set; }
public EnterpriseEnums.SourceSystemName SourceSystemName { get; set; }
public MoveEntity()
{
PrimaryOriginTransferee = new TransfereeEntity();
PrimaryDestinationTransferee = new TransfereeEntity();
Customer = new CustomerEntity();
OriginAddress = new AddressEntity();
DestinationAddress = new AddressEntity();
Order = new OrderEntity();
Shipment = new ShipmentEntity();
}
public bool HasShipment()
{
if (Shipment.ExternalShipmentId > 0)
{
return true;
}
return false;
}
}
Ecco il business Object (MoveEntity) che sto cercando di mappare da
public class MoveEntity
{
public int SourceId { get; set; }
public int MoveId { get; set; }
public bool IsOrderDetailPageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsRoutingPageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsServicePageModified { get; set; } // TODO: Internal - Remove from data contract
public bool IsContentAndContainerPageModified { get; set; } // Rmove from data contract
public string FamilyRange { get; set; } // TODO: Is this being used?
public string Office { get; set; }
public string ActivityType { get; set; }
public string ActivitySubject { get; set; }
public string ActivityNote { get; set; }
public ContactEntity PrimaryOriginTransferee { get; set; }
public ContactEntity PrimaryDestinationTransferee { get; set; }
public CustomerEntity Customer { get; set; }
public AddressEntity OriginAddress { get; set; }
public AddressEntity DestinationAddress { get; set; }
public OrderEntity Order { get; set; }
public ShipmentEntity Shipment { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public string SourceSystemId { get; set; }
public string SourceSystemName { get; set; }
public string Version { get; set; }
public string PortalId { get; set; }
public MoveEntity()
{
PrimaryOriginTransferee = new ContactEntity
{
ContactTypeId = ContactEntity.ContactType.PrimaryOriginationTransferee
};
PrimaryDestinationTransferee = new ContactEntity
{
ContactTypeId = ContactEntity.ContactType.PrimaryDestinationTransferee
};
OriginAddress = new AddressEntity
{
AddressTypeId = AddressEntity.AddressType.Origination
};
DestinationAddress = new AddressEntity
{
AddressTypeId = AddressEntity.AddressType.Destination
};
Order = new OrderEntity();
Customer = new CustomerEntity();
Shipment = new ShipmentEntity();
}
public bool HasShipment()
{
if (Shipment.ShipmentId > 0)
{
return true;
}
return false;
}
}
Le proprietà all'interno di ciascuna classe corrispondono quasi perfettamente per nome, ma i tipi sono diversi. Pertanto ho tentato di eseguire una mappatura personalizzata usando l'espressione "MapFrom". Tuttavia, AutoMapper non sembra in grado di permettermi di puntare da un tipo di oggetto a un altro senza lamentarmi.
Ho anche provato a mappare property-to-property, senza fortuna. Sembrava qualcosa di simile:
.ForMember(dest => dest.PrimaryOriginTransferee.Email, opt => opt.MapFrom(src => src.PrimaryOriginTransferee.Email))
Tuttavia, quando si tenta questo, ricevo il seguente exeception:
deve risolvere al membro di alto livello. Nome parametro: lambdaExpression.
Ho trovato la documentazione disponibile per AutoMapper difficile da seguire. Qualcuno può indicarmi la giusta direzione su come utilizzare correttamente questa utility?
Grazie in anticipo per qualsiasi aiuto!
Adam
Wall O Text ....: ~ ( –
La prossima volta farò in modo che non ci siano informazioni sufficienti Grazie per il tuo contributo – letsgetsilly
Lolz :) :) :) :) –