关键字:<u>面向对象/OOP</u>, <u>封装</u>, <u>重构</u>
项目程序里有如下方法定义:
/// <summary>
/// 将数据批量insert到db表里
/// </summary>
/// <param name="table"></param>
/// <param name="tableColumName"></param>
/// <param name="destinationTableName"></param>
/// <returns></returns>
public bool BulkCopy(DataTable table, string[] tableColumName, string destinationTableName = "T_AlipayRefundRecord")
{
return OrderDataSave.MySqlBulkCopy(table, destinationTableName, tableColumName);
}
通过检测调用代码,发现都是这样的:
var dt = new DataTable();
......
......
string[] sTableColumName = CommonFun.GetColumNameOfDataTable(dt);
// 将退款数据批量insert到dbo.T_AlipayRefundRecord表里
bResult = alipayRefundBll.BulkCopy(dt, sTableColumName);
既然如此,那么,我认为,有必要把获取DataTable列数组的行迁移到BulkCopy方法里,实现封装。
即方法BulkCopy的方法体为:
public bool BulkCopy(DataTable table, string destinationTableName = "T_AlipayRefundRecord")
{
string[] tableColumName = CommonFun.GetColumNameOfDataTable(dt);
return OrderDataSave.MySqlBulkCopy(table, destinationTableName, tableColumName);
}
这样调用的代码就更简洁了:
var dt = new DataTable();
......
......
// 将退款数据批量insert到dbo.T_AlipayRefundRecord表里
bResult = alipayRefundBll.BulkCopy(dt);