Extract methodYou can use an Extract Method refactoring to get rid of the duplicate code:
private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix); }
private static string GetExportFilePath(string csvFilePath, DateTime date, string fileSuffix) { return string.Format("{0}{1:yyyy-MM-dd}-{2}.csv", csvFilePath, date, fileSuffix); }
Also I would add those
selectedExportPathselectedExportType
andcsvFilePathpath
variables as parameters to theExport()
method. I hope they are not used anywhere. It is usually pretty subjective whether to add something as method parameters or leave it as class member, but here I would definitely addpass them as parameters because I also tend to move such methods out from the views and. I assume this class isView
a “View” since you have button click event handlers, and I also tend to move such methods out from the views.
If we use these two ideas, your event handlers would be simplified to:
private void btnBuildingList_Click(object sender, EventArgs e)
{
var path = GetExportFilePath(csvFilePath, datDate.DateTime, "BuildingList.csv");
Export((int)ExportType.Building, path);
}
private void btnOwnerList_Click(object sender, EventArgs e)
{
var path = GetExportFilePath(csvFilePath, datDate.DateTime, "OwnerList.csv");
Export((int)ExportType.Persons, path);
}
private void btnFacts_Click(object sender, EventArgs e)
{
var path = GetExportFilePath(csvFilePath, datDate.DateTime, "FactsData.csv");
Export((int)ExportType.Facts, path);
}