gpt4 book ai didi

c# - AutoMapper - 扁平化具有很长属性名称的类型

转载 作者:行者123 更新时间:2023-11-30 18:24:50 25 4
gpt4 key购买 nike

我正在从事一个需要使用 ebay 网络服务的项目,并且我正在使用他们的 ebay .NET SDK。因为他们的模型和我的模型之间有很多映射,我想我最终会尝试使用 AutoMapper。

这是我正在使用的 ebay API(小部分):

public class SellingManagerProductType {

SellingManagerProductDetailsType SellingManagerProductDetails { get; set; }

SellingManagerProductInventoryStatusType SellingManagerProductInventoryStatus { get; set; }

// other properties
}

public class SellingManagerProductDetailsType {
long ProductID { get; set; }
string ProductName { get; set; }
string CustomLabel { get; set; }
// other properties...
}

public class SellingManagerProductInventoryStatusType {
int QuantityActive { get; set; }
int QuantityScheduled { get; set; }
int QuantitySold { get; set; }
int QuantityUnsold { get; set; }
// other properties..
}

对于这种情况,我的模型是非常简单的 POCO,它只是我在 CRUD 操作中使用的扁平化 SellingManagerProductType。

public class EbaySellingManagerProduct  {
long ProductID { get; set; }
string ProductName { get; set; }
string CustomLabel { get; set; }
int QuantityActive { get; set; }
int QuantityScheduled { get; set; }
int QuantitySold { get; set; }
int QuantityUnsold { get; set; }
}

现在,我想使用 AutoMapper 将 SellingManagerProductType 扁平化为我的 EbaySellingManagerProduct,但如果我正确理解 AutoMapper 默认约定,我将不得不像这样命名我的属性:

long SellingManagerProductDetailsTypeProductID { get; set; }
string SellingManagerProductDetailsTypeProductName { get; set; }
string SellingManagerProductDetailsTypeCustomLabel { get; set; }
int SellingManagerProductInventoryStatusTypeQuantityActive { get;set;}
etc...

观看和使用它是非常痛苦的……而且还有更多此类映射。

我的第一个想法是我可以为这些属性使用 .ForMember,但我最终会映射很多很多属性,使用这个工具几乎不会有任何收获。我还有其他选择可以避免这么长的属性名称吗?

我是 AutoMapper 的新手,非常感谢任何指导。谢谢。

最佳答案

看起来类名的长度很长,不一定是属性。

在这种情况下,您可以使用 using 语句为类命名。

例子:

使用 Details = My.Namespace.SellingManagerProductDetailsType;

在此之后,您可以通过Details 引用类SellingManagerProductDetailsType

这可能会让您的事情变得更短,但它需要在每个代码文件的顶部使用一组千篇一律的 using 语句。

关于c# - AutoMapper - 扁平化具有很长属性名称的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30783777/

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