文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>最简单的重构,你会吗

最简单的重构,你会吗

时间:2010-12-02  来源:小城故事

        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,你一定会,但即使你会,微软都不会去做,你会去做吗?

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载