[英]AutoMapper customization - Mapping strings to DTO
我有以下型號:
public class AccountDto
{
public Guid Id { get; set; }
public string ChamberOfCommerce { get; set; }
public string Code { get; set; }
public VatDto PurchaseVatCode { get; set; }
public VatDto SalesVatCode { get; set; }
}
public class VatDto
{
public string Code { get; set; }
public string Description { get; set; }
public double Percentage { get; set; }
}
public class Account
{
public Guid Id { get; set; }
public string ChamberOfCommerce { get; set; }
public string Code { get; set; }
public string PurchaseVatCode { get; set; }
public string PurchaseVatDescription { get; set; }
public double PurchaseVatPercentage { get; set; }
public string SalesVatCode { get; set; }
public string SalesVatDescription { get; set; }
public double SalesVatPercentage { get; set; }
}
我想以平面模式讀取數據,然后將其映射到AccountDto
。 我嘗試使用以下幾行來配置AutoMapper:
var config = new MapperConfiguration(cfg => {
cfg.AddMemberConfiguration()
.AddMember<NameSplitMember>()
.AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Purchase", "Sales"))
.AddName<ReplaceName>(_ => _.AddReplace("Purchase", string.Empty).AddReplace("Sales", string.Empty));
cfg.RecognizePrefixes("Purchase");
cfg.RecognizePrefixes("Sales");
cfg.RecognizeDestinationPrefixes("Purchase");
cfg.RecognizeDestinationPrefixes("Sales");
cfg.CreateMap<AccountDto, Models.Account>().ReverseMap();
});
var mapper = config.CreateMapper();
var dto = mapper.Map<AccountDto>(account);
但是以上嘗試均無濟於事,我仍然無法映射PurchaseVatCode
。
我收到以下錯誤:
Property:
PurchaseVatCode ---> AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
String -> VatDto (Destination member list)
System.String -> Online.Account.Domain.Dto.VatDto (Destination member list)
Unmapped properties:
Code
Description
TranslatedDescription
Percentage
誰能解決這個問題? 提前致謝!
所有困難都源於AccountDto
屬性名稱PurchaseVatCode
和SalesVatCode
錯誤選擇。 為了使AutoMapper正確拼合名稱,我們需要去除Code
后綴。
問題在於該Account
具有名稱相同但類型不同的屬性。 因此,我們不能使用RecognizeAlias
重命名它們,因為它也會重命名target屬性。
源和目標分開的唯一方法是指定前綴/后綴。 但是,如果我們使用RecognizeSuffix("Code")
以便從源PurchaseVatCode
和SalesVatCode
剝離Code
,那么不幸的是,這VatDto.Code
影響VatDto.Code
屬性(這可能被視為當前的AutoMapper缺陷)。
在這種情況下,您可以做的最好的事情就是使用后者來完成大部分工作並顯式映射Code
屬性:
var config = new MapperConfiguration(cfg =>
{
cfg.RecognizePostfixes("Code");
cfg.CreateMap<AccountDto, Account>()
.ForMember(dst => dst.PurchaseVatCode, opt => opt.MapFrom(src => src.PurchaseVatCode.Code))
.ForMember(dst => dst.SalesVatCode, opt => opt.MapFrom(src => src.SalesVatCode.Code))
.ReverseMap();
});
您需要自定義值解析器 :
CreateMap<Models.Account, AccountDto>()
.ForMember(dest => dest.PurchaseVatCode,
opt => opt.MapFrom(src => new VatDto
{
Code = src.PurchaseVatCode,
Description = src.PurchaseVatDescription,
Percentage = src.PurchaseVatPercentage
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.