电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

最简单的重构,你会吗


发布日期:2022/12/10
 

最近玩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 );

}

这个变化有三点

把IsPostBack条件判断反转减少一层嵌套

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

让注释与上行代码隔一行并单独占一行

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

上一篇:DevExpress的DateEdit控件显示日期的周名

下一篇:同时找到数组中的最小值和最大值