Just getting my feet wet with extension methods and am currently developing some mapping logic to convert an application Invoice to a Quickbooks Invoice. Thought it might be a good idea to have .Convert() extension method.
public static QBInvoice Convert(this InvoiceHeader importedInvoice)
The conversion is just field mapping of one class to another. But then I read somewhere that an extension method is to extend the original class, not convert it to another. Hence why I am asking. I know technically I can do it, but is it best practice compliant or taboo?
Personally I like to use explicit casts so:
public class InvoiceHeader
{
public static explicit operator QBInvoice(InvoiceHeader invoice)
{
return new QBInvoice {}; // do your mapping
}
}
Then you can just cast it:
var qbInvoice = (QBInvoice) invoiceHeader;
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