C#、.Net优化多个 switch 语句实例

项目遇到一个特殊的情况,需要提供一个接口根据传入的值去调用不同的方法,所以导致产生了很多个switch,后面写完想着switch太多了,能不能优化一下,就丢给了chatgpt,下面是优化前和优化后的的代码。

/// <summary>
/// 系统维护通用接口
/// </summary>
/// <returns></returns>
public string SystemMaintenance(CommonSystemMaintenance commonSystemMaintenance)
{
       string result=string.Empty;
        ResultData data = new ResultData();
        switch (commonSystemMaintenance.Type)
        {
            case InterfaceType.HospitalEmployee:
                data = HospitalEmployee(commonSystemMaintenance);
                break;
            case InterfaceType.Settlement:
                data = Settlement(commonSystemMaintenance);
                break;
            case InterfaceType.ModifySettlementFee:
                data = ModifySettlementFee(commonSystemMaintenance);
                break;
            case InterfaceType.ModifySettlementPaymentNumberAndTime:
                data = ModifySettlementPaymentNumberAndTime(commonSystemMaintenance);
                break;
            case InterfaceType.SettlementPaymentMethod:
                data = SettlementPaymentMethod(commonSystemMaintenance);
                break;
            case InterfaceType.ModifySettlementPaymentMethod:
                data = ModifySettlementPaymentMethod(commonSystemMaintenance);
                break;
            case InterfaceType.FinancialPayment:
                data = FinancialPayment(commonSystemMaintenance);
                break;
            case InterfaceType.ModifyFinancialPayment:
                data = ModifyFinancialPayment(commonSystemMaintenance);
                break;
            case InterfaceType.FinancialPaymentDetail:
                data = FinancialPaymentDetail(commonSystemMaintenance);
                break;
            case InterfaceType.ModifyFinancialPaymentDetail:
                data = ModifyFinancialPaymentDetail(commonSystemMaintenance);
                break;
            case InterfaceType.MedicalInsuranceError:
                data = MedicalInsuranceError(commonSystemMaintenance);
                break;
            case InterfaceType.MedicalInsuranceSettlement:
                data = MedicalInsuranceSettlement(commonSystemMaintenance);
                break;
            case InterfaceType.PatientVisitInformation:
                data = PatientVisitInformation(commonSystemMaintenance);
                break;
            case InterfaceType.UpdatePatientVisitAppendix:
                data = UpdatePatientVisitAppendix(commonSystemMaintenance);
                break;
            case InterfaceType.LISData:
                data = LISData(commonSystemMaintenance);
                break;
            case InterfaceType.PACSProject:
                data = PACSProject(commonSystemMaintenance);
                break;
            case InterfaceType.UpdatePACSProject:
                data = UpdatePACSProject(commonSystemMaintenance);
                break;
            case InterfaceType.GoodsStorage:
                data = GoodsStorage(commonSystemMaintenance);
                break;
            case InterfaceType.ModifyGoodsStorage:
                data = ModifyGoodsStorage(commonSystemMaintenance);
                break;
            case InterfaceType.GoodsInStocking:
                data = GoodsInStocking(commonSystemMaintenance);
                break;
            case InterfaceType.AlterGoodsInStock:
                data = AlterGoodsInStock(commonSystemMaintenance);
                break;
            case InterfaceType.GoodsOutStock:
                data = GoodsOutStock(commonSystemMaintenance);
                break;
            case InterfaceType.AlterGoodsOutStock:
                data = AlterGoodsOutStock(commonSystemMaintenance);
                break;
            case InterfaceType.ModifySettlementPaymentNumber:
                data = ModifySettlementPaymentNumber(commonSystemMaintenance);
                break;
            case InterfaceType.PatientVisitAndFeeInformation:
                data = PatientVisitAndFeeInformation(commonSystemMaintenance);
                break;
            case InterfaceType.PrescriptionDetails:
                data = PrescriptionDetails(commonSystemMaintenance);
                break;
        }    
    }
    catch (Exception ex)
    {
        
    }
    return result;
}
/// <summary>
/// 系统维护通用接口
/// </summary>
/// <returns></returns>
public string SystemMaintenance(CommonSystemMaintenance commonSystemMaintenance)
{
    try
    {
        // 创建字典存储处理逻辑
        var handlers = new Dictionary<InterfaceType, Action<CommonSystemMaintenance>>
        {
            { InterfaceType.HospitalEmployee, HospitalEmployee },
            { InterfaceType.Settlement, Settlement },
            { InterfaceType.ModifySettlementFee, ModifySettlementFee },
            { InterfaceType.ModifySettlementPaymentNumberAndTime, ModifySettlementPaymentNumberAndTime },
            { InterfaceType.SettlementPaymentMethod, SettlementPaymentMethod },
            { InterfaceType.ModifySettlementPaymentMethod, ModifySettlementPaymentMethod },
            { InterfaceType.FinancialPayment, FinancialPayment },
            { InterfaceType.ModifyFinancialPayment, ModifyFinancialPayment },
            { InterfaceType.FinancialPaymentDetail, FinancialPaymentDetail },
            { InterfaceType.ModifyFinancialPaymentDetail, ModifyFinancialPaymentDetail },
            { InterfaceType.MedicalInsuranceError, MedicalInsuranceError },
            { InterfaceType.MedicalInsuranceSettlement, MedicalInsuranceSettlement },
            { InterfaceType.PatientVisitInformation, PatientVisitInformation },
            { InterfaceType.UpdatePatientVisitAppendix, UpdatePatientVisitAppendix },
            { InterfaceType.LISData, LISData },
            { InterfaceType.PACSProject, PACSProject },
            { InterfaceType.UpdatePACSProject, UpdatePACSProject },
            { InterfaceType.GoodsStorage, GoodsStorage },
            { InterfaceType.ModifyGoodsStorage, ModifyGoodsStorage },
            { InterfaceType.GoodsInStocking, GoodsInStocking },
            { InterfaceType.AlterGoodsInStock, AlterGoodsInStock },
            { InterfaceType.GoodsOutStock, GoodsOutStock },
            { InterfaceType.AlterGoodsOutStock, AlterGoodsOutStock },
            { InterfaceType.ModifySettlementPaymentNumber, ModifySettlementPaymentNumber },
            { InterfaceType.PatientVisitAndFeeInformation, PatientVisitAndFeeInformation },
            { InterfaceType.PrescriptionDetails, PrescriptionDetails }
        };
        // 获取处理逻辑并执行
        Action<CommonSystemMaintenance> handler;
        if (handlers.TryGetValue(commonSystemMaintenance.Type, out handler))
        {
            handler(commonSystemMaintenance);
        }
        else
        {
            // 处理未找到匹配的情况
            throw new ArgumentException($"未找到处理 {commonSystemMaintenance.Type} 的逻辑。");
        }
    }
    catch (Exception ex)
    {
   
    }
    return "";
}

// 定义处理逻辑的方法
public void HospitalEmployee(CommonSystemMaintenance commonSystemMaintenance)
{
    // 处理 HospitalEmployee 的逻辑
}

public void Settlement(CommonSystemMaintenance commonSystemMaintenance)
{
    // 处理 Settlement 的逻辑
}

// 其他处理逻辑的方法类似

// 定义枚举类型和自定义属性
public enum InterfaceType
{
    [CustomAttribute("HospitalEmployee")]
    HospitalEmployee,

    [CustomAttribute("Settlement")]
    Settlement,

    // 其他枚举值和属性类似
}
posted on 2024-06-14 14:52  一只小小的程序猿  阅读(19)  评论(0编辑  收藏  举报