zoukankan      html  css  js  c++  java
  • 最简单的重构,你会吗

      最近玩Reporting Service报表,微软官方客户端展现实现中有这么一段代码

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    // Get report path from configuration file
                    Uri serverurl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
                    string reportsfolder = ConfigurationManager.AppSettings["SampleReportsPath"];                                           // folder or site path
                    string fileextension = (ConfigurationManager.AppSettings["ReportServerMode"] == "SharePoint" ? ".rdl" : String.Empty);  // extension, depending on the report server mode
                                                                                                                            
                    ReportViewer1.ServerReport.ReportServerUrl = serverurl;
                    ReportViewer1.ServerReport.ReportPath = String.Format("{0}/Company Sales 2008{1}", reportsfolder, fileextension);
                    ReportViewer2.ServerReport.ReportServerUrl = serverurl;
                    ReportViewer2.ServerReport.ReportPath = String.Format("{0}/Employee Sales Summary 2008{1}", reportsfolder, fileextension);
                    ReportViewer3.ServerReport.ReportServerUrl = serverurl;
                    ReportViewer3.ServerReport.ReportPath = String.Format("{0}/Product Catalog 2008{1}", reportsfolder, fileextension);
                    ReportViewer4.ServerReport.ReportServerUrl = serverurl;
                    ReportViewer4.ServerReport.ReportPath = String.Format("{0}/Sales Trend 2008{1}", reportsfolder, fileextension);
                    ReportViewer5.ServerReport.ReportServerUrl = serverurl;
                    ReportViewer5.ServerReport.ReportPath = String.Format("{0}/Territory Sales Drilldown 2008{1}", reportsfolder, fileextension);
                }
            }
    

      忍不住手痒一下,把它变成:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack) return;
    
                // Get report path from configuration file
                Uri serverurl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
                string reportsfolder = ConfigurationManager.AppSettings["SampleReportsPath"];
    
                // folder or site path. extension, depending on the report server mode
                string fileextension = (ConfigurationManager.AppSettings["ReportServerMode"] == "SharePoint" ? ".rdl" : String.Empty);
    
                Action<Microsoft.Reporting.WebForms.ReportViewer, string> setReportViewer =
                    (report, reportName) =>
                    {
                        report.ServerReport.ReportServerUrl = serverurl;
                        report.ServerReport.ReportPath = String.Format("{0}/{2}{1}", reportsfolder, fileextension, reportName);
                    };
    
                setReportViewer(ReportViewer1, "Company Sales 2008");
                setReportViewer(ReportViewer2, "Employee Sales Summary 2008");
                setReportViewer(ReportViewer3, "Product Catalog 2008");
                setReportViewer(ReportViewer4, "Sales Trend 2008");
                setReportViewer(ReportViewer5, "Territory Sales Drilldown 2008");
            }
    

      这个变化有三点:

      1、把IsPostBack条件判断反转,减少一层嵌套。

      2、把设置ReportViewer的相同逻辑提取出来,在一个委托中设置。

      3、让注释与上行代码隔一行,并单独占一行。

      应该再简单没有了吧,但是,你会吗?Sorry,你一定会,但即使你会,微软都不会去做,你会去做吗?

  • 相关阅读:
    基于 abp vNext 和 .NET Core 开发博客项目
    基于 abp vNext 和 .NET Core 开发博客项目
    基于 abp vNext 和 .NET Core 开发博客项目
    基于 abp vNext 和 .NET Core 开发博客项目
    数据结构 6 基础排序算法详解 冒泡排序、三层冒泡排序逐步优化方案详解
    数据结构 5 哈希表/HashMap 、自动扩容、多线程会出现的问题
    数据结构 4 时间复杂度、B-树 B+树 具体应用与理解
    数据结构 3 二叉查找树、红黑树、旋转与变色 理解与使用
    数据结构 2 字符串 数组、二叉树以及二叉树的遍历
    数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解
  • 原文地址:https://www.cnblogs.com/XmNotes/p/1894124.html
Copyright © 2011-2022 走看看