熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

最簡單的重構,你會嗎

2013-11-13 10:15:22  來源: .NET編程 

  最近玩Reporting Service報表微軟官方客戶端展現實現中有這麼一段代碼

   protected void Page_Load(object sender EventArgs e)

   {

       if (!IsPostBack)

       {

           // Get report path from configuration file

           Uri serverurl = new Uri(ConfigurationManagerAppSettings[ReportServerUrl]);

           string reportsfolder = ConfigurationManagerAppSettings[SampleReportsPath];                                           // folder or site path

           string fileextension = (ConfigurationManagerAppSettings[ReportServerMode] == SharePoint ? rdl : StringEmpty);  // extension depending on the report server mode

  

           ReportViewerServerReportReportServerUrl = serverurl;

           ReportViewerServerReportReportPath = StringFormat({}/Company Sales {} reportsfolder fileextension);

           ReportViewerServerReportReportServerUrl = serverurl;

           ReportViewerServerReportReportPath = StringFormat({}/Employee Sales Summary {} reportsfolder fileextension);

           ReportViewerServerReportReportServerUrl = serverurl;

           ReportViewerServerReportReportPath = StringFormat({}/Product Catalog {} reportsfolder fileextension);

           ReportViewerServerReportReportServerUrl = serverurl;

           ReportViewerServerReportReportPath = StringFormat({}/Sales Trend {} reportsfolder fileextension);

           ReportViewerServerReportReportServerUrl = serverurl;

           ReportViewerServerReportReportPath = StringFormat({}/Territory Sales Drilldown {} reportsfolder fileextension);

       }

   }

  忍不住手癢一下把它變成

   protected void Page_Load(object sender EventArgs e)

   {

       if (IsPostBack) return;

  

       // Get report path from configuration file

       Uri serverurl = new Uri(ConfigurationManagerAppSettings[ReportServerUrl]);

       string reportsfolder = ConfigurationManagerAppSettings[SampleReportsPath];

  

       // folder or site path extension depending on the report server mode

       string fileextension = (ConfigurationManagerAppSettings[ReportServerMode] == SharePoint ? rdl : StringEmpty);

  

       Action<MicrosoftReportingWebFormsReportViewer string> setReportViewer =

           (report reportName) =>

           {

               reportServerReportReportServerUrl = serverurl;

               reportServerReportReportPath = StringFormat({}/{}{} reportsfolder fileextension reportName);

           };

  

       setReportViewer(ReportViewer Company Sales );

       setReportViewer(ReportViewer Employee Sales Summary );

       setReportViewer(ReportViewer Product Catalog );

       setReportViewer(ReportViewer Sales Trend );

       setReportViewer(ReportViewer Territory Sales Drilldown );

   }

  這個變化有三點

  1把IsPostBack條件判斷反轉減少一層嵌套

  把設置ReportViewer的相同邏輯提取出來在一個委托中設置

  讓注釋與上行代碼隔一行並單獨占一行

  應該再簡單沒有了吧但是你會嗎?Sorry你一定會但即使你會微軟都不會去做你會去做嗎?


From:http://tw.wingwit.com/Article/program/net/201311/13073.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.