gpt4 book ai didi

c# - AutoMapper 定制 - 将字符串映射到 DTO

转载 作者:行者123 更新时间:2023-11-30 21:34:56 25 4
gpt4 key购买 nike

我有以下模型:

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 属性名称 PurchaseVatCodeSalesVatCode 的错误选择。为了让 AutoMapper 正确地拼合名称,我们需要去掉 Code 后缀。

问题是 Account 具有名称相同但类型不同的属性。所以我们不能使用 RecognizeAlias 来重命名它们,因为它也会重命名目标属性。

源和目标唯一不同的方法是指定前缀/后缀。但是,如果我们使用 RecognizeSuffix("Code") 从源代码 PurchaseVatCodeSalesVatCode 中去除 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();
});

关于c# - AutoMapper 定制 - 将字符串映射到 DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com