Consider I have these classes in my Api model, because other actions may need customerData with different attributes I hide base properties like this:
public class CustomerData
{
public string Name { get; set; }
public string PublicKey { get; set; }
}
and this class:
public class AddCustomerInput:CustomerData
{
[Required]
public new string Name { get; set; }
[Required]
public new string PublicKey { get; set; }
}
these two models are in Api level and I have another model in domain level:
public class ExternalCustomerData
{
public new string CustomerName{ get; set; }
public new string PublicKey { get; set; }
}
as you can see name here is customerName, also these models are big with different names in api and domain model names but I shortened the models,
also I have this extension method to convert types:
public static ExternalCustomerData ToExternalCustomerData(this CustomerData customerData)
{
//All properties with same property names convert correctly
var externalCustomerData =customerData.ConvertTo<ExternalCustomerData>();
//but the result of customerData.Name is null
externalCustomerData .CustomerName= customerData.Name ;
return externalCustomerData ;
}
when I use this extension method for AddCustomerInput:
addCustomerInputObject.ToExternalCustomerData();
I see All properties with same property names convert correctly but this line:
externalCustomerData .CustomerName= customerData.Name
is null. I was wondering what is the reason servicestack cast this parent object to child correcly, how can I achieve change in name? I want to know is it possible to get the result with servicestack.text convertTo? If not is there any good solution for doing this?
*I know i can have
public static ExternalCustomerData ToExternalCustomerData(this AddCustomerInput customerData)
{
}
and it works fine but I have many apiInput models which all of them inherit from CustomerData and I should have many extension methods.
No ServiceStack's Auto Mapping doesn't support mapping properties with different names, the recommendation is to use Extension Methods to handle any additional customization's needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With