mercredi 24 mars 2021

Need help in re-factoring if else conditions in C#

I have one class file where I have user if else if and with multiple conditions -However I feel class file is really confusing, so can you help me if I can refactor the code any which way?

   public static PaymentOptions GetPaymentOptions_Auto(TestConfigurationCDO testConfiguration, int siteId)
    {
        var paymentOptions = new PaymentOptions();

        var paymentOptionList = SitePaymentRepository.GetSitePaymentInfoBySiteId(
            testConfiguration,
            siteId);

        var lowestPriority = paymentOptionList.Min(x => x.Priority);
        var paymentAuto = paymentOptionList.Where(x => x.Priority == lowestPriority).FirstOrDefault();
        
        if (paymentAuto.PaymentType == PaymentMethod.Klarna)
        {
            paymentOptions = new KlarnaOptions();
        }
        else if (paymentAuto.PaymentType == PaymentMethod.PayPalDirect ||
            paymentAuto.PaymentType == PaymentMethod.Braintree ||
        paymentAuto.PaymentType == PaymentMethod.BankTransfer || paymentAuto.PaymentType == PaymentMethod.AdyenDropIn)
        {
            paymentOptions = new ClientCheckoutOptions()
            {
                paymentMethod = paymentAuto.PaymentType
            };
        }
        else if (paymentAuto.PaymentType == PaymentMethod.PayPalExpress)
        {
            paymentOptions = new PaypalOptions();
        }
        else
        {
            paymentOptions = new PaypalOptions();
        }
        return paymentOptions;
    }

Aucun commentaire:

Enregistrer un commentaire