dimanche 29 septembre 2019

Code refactor to replace throw exception technique with operation result

I need to do some refactoring for the below code as i am looking replacing the throw invalidoperationexception technique with another one returning an operationresult object ,also i need to return a json object. i need your advice regarding refactoring the below code in general as well.

looking for several design patterns , and i got an idea of replacing throw expectation with returning an object with boolean flag if operation is succeed or not , and a string with Error message if any exception rules.

//Method to return json with operation result public void UpdateNINByID(int ID) { try { m_logger.Log(LogLevel.Info, string.Format("UpdateNINByID is Started for ID {0}", ID));

            o_tdsAlert = new tdsAlert();
            o_dmAlert.GetSrcDestDataByID(o_tdsAlert, ID);

            if (o_tdsAlert.DATA_SEARCH_EFGHEGCA_SI_HAS_ACC.Count == 0)
            {
                m_logger.Log(LogLevel.Trace, string.Format("UpdateNINByID - No data found in source for ID {0}", ID));
                return;
            }

            ConvertNinToUpperCase(o_tdsAlert);

            var drSrc = o_tdsAlert.DATA_SEARCH_EFGHEGCA_SI_HAS_ACC.FirstOrDefault();

//I need to refactor this oALRTNINExtractionConfig = oALRTNINExtractionConfig.GetALRTNINConfig(drSrc.Country, drSrc.MethodType);

            SourceData srcIDs = CreateValidListSrcList(drSrc, drSrc.MethodType);
            List<CleansedIDs> cleansedIDs = GetCleansedIDs(drSrc, srcIDs);
            TransformToDTCC_Client_DATA(o_tdsAlert, drSrc, cleansedIDs);

            o_dmAlert.updateDtcc_Client_DataByID(o_tdsAlert);

            if (oALRTNINExtractionConfig.IsHandleAlertRowUpdates)
                AlertCleansing();

            if (oALRTNINExtractionConfig.IsSendNotification)
                SendNotification(srcIDs);

            m_logger.Log(LogLevel.Info, string.Format("UpdateNINByID - is Finished for ID {0}", ID));
        }
        catch (Exception ex)
        {
            m_logger.LogException(LogLevel.Error, "UpdateNINByID - NIN ByID Exception", ex);
            throw;
        }
    }

//I need to refactor public ALRTNINExtractionConfig GetALRTNINConfig(string Country, string MethodType) { var ALRTNINExtractionConfigs = JsonConvert.DeserializeObject( File.ReadAllText(ConfigurationManager.AppSettings["ALRTNINExtractionConfig"])); if (ALRTNINExtractionConfigs == null) throw new InvalidOperationException("ALRTNINExtractionConfigs is null");

        IEnumerable<ALRTNINExtractionConfig> oALRTNINExtractionConfiglst = ALRTNINExtractionConfigs.ALRTNINExtractionConfiglst;

        if (!oALRTNINExtractionConfiglst.Where(r => r.Country == Country).Any())
            Country = MethodType = Default;

        oALRTNINExtractionConfiglst = oALRTNINExtractionConfiglst.Where(r => r.Country == Country);
        if (!oALRTNINExtractionConfiglst.Any())
            throw new InvalidOperationException("Country isnot set in configuration");
        if (!oALRTNINExtractionConfiglst.Where(r => r.MethodType.Contains(MethodType)).Any())
            throw new InvalidOperationException("MethodType isnot set in configuration");
        var aLRTNINExtractionConfig = oALRTNINExtractionConfiglst.Where(r => r.MethodType.Contains(MethodType)).FirstOrDefault();
        return aLRTNINExtractionConfig;
    }

I am excepting some code refactor recommendations , also i can share the whole code if needed.

Aucun commentaire:

Enregistrer un commentaire