2012-10-26 133 views
0

給定以下代碼,是否有無法使Automapper執行此操作而不是手動設置屬性?我試圖避免必須在Mapper.CreateMap上執行一對一的映射,因爲我不斷地從Contract中添加和刪除字段,並且此刻必須記住用這些更改來更新此方法。使用automapper從dto反向映射

private void MapModelToContract(ContractDto model, Contract contract) 
     { 
      contract.Id = model.Id; 
      contract.ContractDetail.BuyerCornhouseName = model.ContractDetailBuyerCornhouseName; 
      contract.ContractDetail.CnoCornLtdToBuyer = model.ContractDetailCnoCornLtdToBuyer; 
      contract.ContractDetail.CnoCornPartToCornLtd = model.ContractDetailCnoCornPartToCornLtd; 
      contract.ContractDetail.CnoSellerCornLtd = model.ContractDetailCnoSellerCornLtd; 
      contract.ContractDetail.CnoSellerCornPart = model.ContractDetailCnoSellerCornPart; 
      contract.ContractDetail.Commission = model.ContractDetailCommission; 
      contract.ContractDetail.ContractPrice = model.ContractDetailContractPrice; 
      contract.ContractDetail.SalesPriceToCornLtd = model.ContractDetailSalesPriceToCornLtd; 
      contract.ContractDetail.ContractedQuantity = model.ContractDetailContractedQuantity; 
      contract.ContractDetail.CostWarehouseToFob = model.ContractDetailCostWarehouseToFob; 
      contract.ContractDetail.Date = model.ContractDetailDate; 
      contract.ContractDetail.DeliveryDate = model.ContractDetailDeliveryDate; 
      contract.ContractDetail.FinalBuyerName = model.ContractDetailFinalBuyerName; 
      contract.ContractDetail.FinalSalesPrice = model.ContractDetailFinalSalesPrice; 
      contract.ContractDetail.Grade = model.ContractDetailGrade; 
      contract.ContractDetail.Notes = model.ContractDetailNotes; 
      contract.ContractDetail.PayableQuantity = model.ContractDetailPayableQuantity; 
      contract.ContractDetail.SalesShipmentPeriod = model.ContractDetailSalesShipmentPeriod; 
      contract.ContractDetail.SellerName = model.ContractDetailSellerName; 
      contract.ContractDetail.ToBePaidBy = model.ContractDetailToBePaidBy; 
      contract.ContractDetail.SalesBasis = model.ContractDetailSalesBasis; 
      contract.ContractDetail.SalesFreightValue = model.ContractDetailSalesFreightValue; 

      contract.CurrencyContract.Date = model.CurrencyContractDate; 
      contract.CurrencyContract.ExchangeRate = model.CurrencyContractExchangeRate; 
      contract.CurrencyContract.Number = model.CurrencyContractNumber; 
      contract.CurrencyContract.Value = model.CurrencyContractValue; 
      contract.CurrencyContract.IsAcc = model.CurrencyContractIsAcc; 
      contract.CurrencyContract.RepaidOn = model.CurrencyContractRepaidOn; 
      contract.CurrencyContract.RepaymentDate = model.CurrencyContractRepaymentDate; 

      contract.Discounts.Reason = model.DiscountsReason; 
      contract.Discounts.Value = model.DiscountsValue; 

      contract.DocumentControl.GuaranteeLetterReceived = model.DocumentControlGuaranteeLetterReceived; 
      contract.DocumentControl.PhotosReceived = model.DocumentControlPhotosReceived; 

      contract.GoodsStatus.ContainerNumber = model.GoodsStatusContainerNumber; 
      contract.GoodsStatus.EtaPort = model.GoodsStatusEtaPort; 
      contract.GoodsStatus.ExpectedDeliveryDate = model.GoodsStatusExpectedDeliveryDate; 
      contract.GoodsStatus.Port = model.GoodsStatusPort; 
      contract.GoodsStatus.ShippedStatus = model.GoodsStatusShippedStatus; 
      contract.GoodsStatus.VesselDeadline = model.GoodsStatusVesselDeadline; 
      contract.GoodsStatus.VesselName = model.GoodsStatusVesselName; 
      contract.GoodsStatus.HasSellerConfirmedRelease = model.GoodsStatusHasSellerConfirmedRelease; 
      contract.GoodsStatus.ReceivedShippingInstructions = model.GoodsStatusReceivedShippingInstructions; 
      contract.GoodsStatus.Notes = model.GoodsStatusNotes; 

      contract.NotaFiscalCornPart.Date = model.NotaFiscalCornPartDate; 
      contract.NotaFiscalCornPart.ExchangeRate = model.NotaFiscalCornPartExchangeRate; 
      contract.NotaFiscalCornPart.Number = model.NotaFiscalCornPartNumber; 

      contract.NotaFiscalSeller.Date = model.NotaFiscalSellerDate; 
      contract.NotaFiscalSeller.GoodsReleased = model.NotaFiscalSellerGoodsReleased; 
      contract.NotaFiscalSeller.Number = model.NotaFiscalSellerNumber; 

      contract.TransferToSeller.Date = model.TransferToSellerDate; 
      contract.TransferToSeller.TransferredFrom = model.ContractDetailBuyerCornhouseName; 
      contract.TransferToSeller.Value = model.TransferToSellerValue; 

      contract.CommissionLawyer.IsPaid = model.CommissionLawyerIsPaid; 
     } 
+0

而這不起作用? 'Mapper.CreateMap ()' – danludwig

回答

1

下面的mapper適合你。

如果源(ContractDto)和目標(Contract)具有相同的屬性名稱。

Mapper.CreateMap<ContractDto, Contract>(); // Maps if ContractDto & Contract have same property names 

如果源(ContractDto)和目標(Contract)有差異的屬性名。然後

Mapper.CreateMap<ContractDto, Contract>() 
    .ForMember(destination => destination.BuyerCornhouseName, options => options.MapFrom(source => source.ContractDetailBuyerCornhouseName)) 
    .ForMember(destination => destination.CnoCornLtdToBuyer, options => options.MapFrom(source => source.ContractDetailCnoCornLtdToBuyer)); 

    // Explicitly you have specify for other properties also