Skip to main content

Refactor large switch case with design pattern

I am trying to build a report based on certain condition. The problem is that I have a massive switch statement which is a "code smell".

How can refactor the following so that it is more maintainable?

I just copied/paste some of the values to show how the case will look.

switch (countryId)
{
    case 1:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 4:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N");
        break;
    case 23:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 20:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 25:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 27:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 14:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 31:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 30:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
    case 28:
        reportParameterValues = TaxReportParameterFactory.CreateReport(taxCountry, employeeId, taxYearId, frequencyId, null, "N", employeeRun.RunId, 0, 0, 0, true);
        break;
}


public class TaxCounty
{
    public string CountryCode { get; set; }

    public string ReportPath { get; set; }

    public string[] ReportParameters { get; set; }
}

public static class TaxCountries
{
    public static Dictionary<int, TaxCounty> Type => 
        new Dictionary<int, TaxCounty>
            {
                { 1, new TaxCounty { CountryCode = "SA", ReportPath = "/PathToReport", ReportParameters = new[] { "EmployeeID", "BureauTaxID", "FrequencyID", "DirectiveNumber", "Directive", "RunID", "UserID", "GroupID", "RegionID", "IncludeESS" } } },
                { 4, new TaxCounty { CountryCode = "SWZ", ReportPath = "/PathToReport", ReportParameters = new[] { "EmploymentStatusID", "BureauTaxID", "FrequencyID", "RunID" } } },
                { 23, new TaxCounty { CountryCode = "BOTS", ReportPath = "/PathToReport", ReportParameters = new[] { "EmploymentStatusID", "BureauTaxID", "FrequencyID", "RunID" } } },
                { 20, new TaxCounty { CountryCode = "MOZ", ReportPath = "/PathToReport", ReportParameters = new[] { "EmploymentStatusID", "BureauTaxID", "FrequencyID", "RunID" } } },
                { 25, new TaxCounty { CountryCode = "Tanzania", ReportPath = "/PathToReport", ReportParameters = new[] { "EmployeeID", "BureauTaxID" } } },
                { 27, new TaxCounty { CountryCode = "NAM", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "FrequencyID" } } },
                { 14, new TaxCounty { CountryCode = "GHANA", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "FrequencyID", "fkEmpStatusID", "CompanyID" } } },
                { 31, new TaxCounty { CountryCode = "MALA", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "FrequencyID" } } },
                { 30, new TaxCounty { CountryCode = "ZIM", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "FrequencyID", "EmpStatus" } } },
                { 28, new TaxCounty { CountryCode = "MAU", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "FrequencyID" } } },
                { 12, new TaxCounty { CountryCode = "GAB", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "CompanyID" } } },
                { 37, new TaxCounty { CountryCode = "CONGO", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "CompanyID" } } },
                { 49, new TaxCounty { CountryCode = "LESO", ReportPath = "/PathToReport", ReportParameters = new[] { "BureauTaxID", "EmploymentStatusID", "FrequencyID" } } },
                { 22, new TaxCounty { CountryCode = "KENYA", ReportPath = "/PathToReport", ReportParameters = new[] { "CompanyID", "EmpID", "TaxYearID", "CompanyFrequencyID" } } }
            };
}

I have a Dictionary that holds all the information so that I can do an easy lookup. Then I use the factory map the parameters. But what a maintance nightmare.

public static class TaxReportParameterFactory
{
    public static Dictionary<string, object> CreateReport(TaxCounty taxCountry, params object[] values)
    {
        var reportParameterValues = taxCountry.ReportParameters.Zip(values, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v);
        reportParameterValues.Add("ReportPath", taxCountry.ReportPath);

        return reportParameterValues;
    }
}
R4nc1d
  • 361
  • 1
  • 12