Replacement for a large switch?

I have a page called ReportController.aspx, the purpose of which is to create an instance of the report (class) based on the query string parameters

        switch (Request.QueryString["Report"])
        {                
            case "ReportA":
                CreateReportAReport("ReportA Title");
                break;
            case "ReportB":
                CreateReportBReport("ReportB Title");
                break;                
            case "ReportC":
                CreateReportCReport("ReportC Title");
                break;
            case "ReportD":
                CreateReportDReport("ReportD Title");
                break;
                ...

Basically, every time a new report is required, this is the overhead of adding a case and adding a method. This switch statement can be very long. I read that you can use a dictionary to map a report. How it would look with the help of the Dictionary (assuming this is the best way).

In addition, the method CreateReportXReportbasically passes a bunch of additional QueryString values ​​to the report class constructor (each report class has a different constructor).

+5
source share
4 answers

, IReport, Func<IReport>, :

IDictionary<string,Func<IReport>> dictToReport = new Dictionary {
    {"ReportA", () => CreateReportAReport("ReportA Title") }
,   {"ReportB", () => CreateReportBReport("ReportB Title") }
,   ...
};

:

var myReport = dictToReport[Request.QueryString["Report"]]();
+4

-; , , .

- XML , , .

, , . :

  • ,

, :

Dictionary<string, IReportCreator> = configDataGetter.GetReportDataFromDB().
    ToDictionary(r => r.Name, myReportCreatorFactory(r => r.ReportID))

- - factory, . , , .

, , , sql styling db?

op:

, . , , , - factory, . , , , , , . , , . , , , , , , , , .

-, db ( ), db ( ).

, , , . " " . ReportGenerator ( IReportGenerator). ; . , ( db), /.

, select factory, reflection. db ( ).

, . . .

, / " ". MEF. . , , , , , CodeDom (, , MEF ), , .NET 5. MEF - .

+5

, ( "" ), .

.

+1

Dictionary<string, string>,

public class Container {
  private static Dictionary<string, Func<Report>> ReportMap = 
    new Dictionary<string, Func<Report>>();
  static Container() {
    ReportMap["ReportA"] = () => CreateReportAReport("ReportA Title");
    ReportMap["ReportB"] = () => CreateReportBReport("ReportB Title");
    // etc ...
  }
}

Now that the map is built, you simply search in the function instead switch

Func<Report> func;
if (!ReportMap.TryGetValue(Request.QueryString["Report"), out func)) {
  // Handle it not being present
  throw new Exception(..);
}

Report report = func();
+1
source

All Articles