0

I have a web[forms] app that uses a reportviewer. In the code behind, I have code that looks like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

        Dim u As New UriTypeConverter

        Dim vSDate, vEDate As Date
        vSDate = Session("StartDate")
        vEDate = Session("EndDate")

        If Session("ReportName") = "Report1" Then
            Dim reportparameters(1) As ReportParameter
            reportparameters(0) = New ReportParameter("StartDate", vSDate)
            reportparameters(1) = New ReportParameter("EndDate", vEDate)
            ReportViewer1.ServerReport.ReportPath = "/SSRS01/Report1"
            ReportViewer1.ServerReport.SetParameters(reportparameters)
        End If

        If Session("ReportName") = "Report2" Then
            Dim reportparameters(1) As ReportParameter
            reportparameters(0) = New ReportParameter("StartDate", vSDate)
            reportparameters(1) = New ReportParameter("EndDate", vEDate)
            reportparameters(2) = New ReportParameter("Product", Session("ProductName"))
            ReportViewer1.ServerReport.ReportPath = "/SSRS01/Report2"
            ReportViewer1.ServerReport.SetParameters(reportparameters)
        End If

And there are about 12 if statements, some having different number of parameters than others. So as opposed to having to write more if statements with varying parameters. I wonder if there's a structure, technique, pattern that would allow me get around writing if statements for every new report I create. And I don't care what's needed. Database, new library, learn 5 new GoF patterns. Whatever. Only requirement/restriction is that this is [obviously] a .NET app.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
  • 2
    Would a dictionary to an action work? I am not familiar with VB.NET syntax enough to show you an example, but in C# it would look like `IDictionary>`, with lambdas corresponding to each `If` statement. – Sergey Kalinichenko Jun 11 '15 at 18:19
  • Those "if" statements (or equivalents thereof) are going to exist. *And that's OK.* When they exist more than once, that's when you step back and refactor and allow a pattern to emerge. So with that said, is this snippet *the one* or *the one of many?* – Anthony Pegram Jun 11 '15 at 18:22
  • @dasblinkenlight - I'm not familar with the Action data structure especially in Webforms. – dotnetN00b Jun 11 '15 at 18:23
  • @dotnetN00b [`Action` delegate](https://msdn.microsoft.com/en-us/library/018hxwa8%28v=vs.110%29.aspx). – Sergey Kalinichenko Jun 11 '15 at 18:24
  • @AnthonyPegram - I've shown 2 of 12 if statements. Each if statement points to a different report. Each report could have two or more reportparameters. – dotnetN00b Jun 11 '15 at 18:25
  • @dotnetNoob, what I mean is, it doesn't matter how many different statements there are *in one place*, it's how many places those different statements are repeated. When you start duplicating this logic, that's when the need to refactor increases. – Anthony Pegram Jun 11 '15 at 18:29
  • I always start with getting rid of repeating code, and then go to anything that I have hard coded that could be a variable instead. Based on this example you provided you could cut each if block down to as little as 0 lines if you don't have more than start and end date as parameters. This is under an assumption that all blocks will use the start and end date. – MaCron Jun 11 '15 at 19:03
  • @MaCron - problem is that there are more than start and end date as parameters depending on the report. Of which there are 12. – dotnetN00b Jun 11 '15 at 19:13
  • Are the start and end dates part of every call though? That is the important part of this, that means you can pull those out of any checks, and based on your sample there are some sections that only have start and end times, you then only add to the parameter list when its necessary and make a single call at the end based on the value of ReportName. – MaCron Jun 15 '15 at 19:43
  • @MaCron - you are right about the start and end dates. Those can be added at the end. – dotnetN00b Jun 17 '15 at 13:14
  • you can also move `reportpath = ""` and 'setparameters=""` to the end since it appears the only thing that changes in the path is the report name, which you are getting in the session. – MaCron Jun 17 '15 at 14:55

1 Answers1

1

Your best bet is to replace multiple If or long switch statements with a Strategy Pattern. Good explanation on how to can be found here

another good example you will find in this discussion on stackoverflow - it is on Java, but logic is very clear and can be ported in any .Net language Converting many 'if else' statements to a cleaner approach

Community
  • 1
  • 1
Yuri
  • 2,820
  • 4
  • 28
  • 40