Hi All I have two request DTOs, of which one is the parent and one is the child.
public class PaymentSupplierPaymentRequest
{
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("amount")]
[Required]
public decimal Amount { get; set; }
[JsonProperty("currency")]
public string Currency { get; set; }
[JsonProperty("supplierid")]
[Required]
public string SupplierId { get; set; }
[JsonProperty("accountid")]
[Required]
public long AccountId { get; set; }
}
public class PaymentSupplierLitePaymentRequest: PaymentSupplierPaymentRequest
{
[JsonProperty("merchantCode")]
[Required]
public string MerchantCode { get; set; }
}
There are two types of payment request one which the Merchant Code is required and one where the merchant code is not required. Depending on the request I have another class that uses two constructors to handle the data passed into the request.
private readonly string _merchantCode;
private readonly string _installationId;
private readonly string _version;
private readonly string _paymentMethodMaskCode;
private readonly string _orderDescription;
private readonly long _amount;
private readonly string _currency;
private readonly string _email;
private const byte Exponent = 2;
private readonly string _orderCode;
public GemPayPaymentRequestBuilder(PaymentSupplierLitePaymentRequest paymentRequest)
{
_amount = (long)(paymentRequest.Amount * (decimal)Math.Pow(10, Exponent));
_orderDescription = paymentRequest.Description;
_currency = paymentRequest.Currency;
_orderCode = $"{paymentRequest.AccountId}_{DateTime.UtcNow.Ticks}";
_merchantCode = paymentRequest.MerchantCode;
}
public GemPayPaymentRequestBuilder(PaymentSupplierPaymentRequest paymentRequest, string email, WorldPayMerchantConfig config)
{
_amount = (long)(paymentRequest.Amount * (decimal)Math.Pow(10, Exponent));
_orderDescription = paymentRequest.Description;
_currency = paymentRequest.Currency;
_orderCode = $"{paymentRequest.AccountId}_{DateTime.UtcNow.Ticks}";
_email = email;
_merchantCode = config.MerchantCode;
_installationId = config.InstallationId;
_version = config.Version;
_paymentMethodMaskCode = config.PaymentMethodMaskCode;
}
The problem is as you can see in the code I am duplicating the initialising of certain local variables in both constructors.
_amount = (long)(paymentRequest.Amount * (decimal)Math.Pow(10, Exponent));
_orderDescription = paymentRequest.Description;
_currency = paymentRequest.Currency;
_orderCode = $"{paymentRequest.AccountId}_{DateTime.UtcNow.Ticks}";
Does anyone have a cleaner way of doing this? I have tried using constructor inheritance for the second constructor as shown below.
public GemPayPaymentRequestBuilder(PaymentSupplierLitePaymentRequest paymentRequest)
{
_amount = (long)(paymentRequest.Amount * (decimal)Math.Pow(10, Exponent));
_orderDescription = paymentRequest.Description;
_currency = paymentRequest.Currency;
_orderCode = $"{paymentRequest.AccountId}_{DateTime.UtcNow.Ticks}";
_merchantCode = paymentRequest.MerchantCode;
}
public GemPayPaymentRequestBuilder(PaymentSupplierPaymentRequest paymentRequest, string email, WorldPayMerchantConfig config) : this(paymentRequest)
{
_email = email;
_merchantCode = config.MerchantCode;
_installationId = config.InstallationId;
_version = config.Version;
_paymentMethodMaskCode = config.PaymentMethodMaskCode;
}
But this does not work as using : this(paymentRequest) throws a conversion error as it cannot convert from PaymentSupplierPaymentRequest to PaymentSupplierLitePaymentRequest.
Any Ideas?
PaymentSupplierPaymentRequesttoPaymentSupplierLitePaymentRequestand usethis(ConversionMethod(paymentRequest)), or create an implicit cast operator onoverload on one of the two types to convert from one to the other.: this((PaymentSupplierLitePaymentRequest)paymentRequest)