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

标签 c# automapper

我正在从事一个需要使用 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/

相关文章:

c# - IQueryable 没有 Count()?

c# - 展平嵌套对象以将其属性映射到目标对象

AutoMapper 解析器未传递预期类型

c# - AutoMapper 映射函数到成员的获取

c# - 使用 "as"运算符测试 Page.RouteData.Values 中的整数类型

c# - 如何回答请求但继续处理 WebApi 中的代码

c# - AutoMapper - 一对多映射

interface - 自动映射器 : Mapping to an Interface

c# - Enum 可以包含其他 Enum 吗?

c# - 如何创建接受多个值并序列化为 WinForms Form.designer.cs 的 TypeConverter?