AutoMapper - Flattening types with very long property names

178 views Asked by At

Im working on a project that needs to consume ebay web service and I'm using their ebay .NET SDK. And because there is a lot of mapping going on between their model and my model, I thought I will finally give AutoMapper a try.

So this is the ebay API im working with (small part):

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.. 
} 

For this case my model is very simple POCO, it's just flattened SellingManagerProductType I use in CRUD operations.

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; }
}

Now, I would like to flatten SellingManagerProductType to my EbaySellingManagerProduct using AutoMapper, but if I understand AutoMapper default convention correctly I would have to name my properties like this:

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

which is very painful to watch and work with... And there is a lot more of these type of mappings.

My first thought was I could use .ForMember for these properties but I would end up mapping lots and lots of them and would have had hardly any gains using this tool. Are there any other options for me to avoid this long property names?

I'm very new to AutoMapper and any guidance will be greatly appreciated. Thanks.

1

There are 1 answers

0
Khan On BEST ANSWER

It looks like the class names are long in length, not necessarily the properties.

In this case you can alias the class names with using statements.

Example:

using Details = My.Namespace.SellingManagerProductDetailsType;

After this, you can refer to the class SellingManagerProductDetailsType by Details.

This could make things shorter for you, but it would require a cookie cutter group of using statements at the top of each of your code files.