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

AutoMapper - Flattening types with very long property names

我正在做一个需要使用 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 的新手,非常感谢任何指导。谢谢。

看起来 class 名称很长,不一定是属性。

在这种情况下,您可以使用 using 语句为 class 名称起别名。

示例:

using Details = My.Namespace.SellingManagerProductDetailsType;

在此之后,您可以参考class SellingManagerProductDetailsType by Details

这可能会缩短您的时间,但需要在每个代码文件的顶部使用 using 语句的千篇一律组。