我一直在使用.NET表适配器和数据表,我一直在努力解决如何模块化这些代码,但我无法“看到”它。 这就是我所拥有的,但必须有一种方法可以组合成一个处理这两种情况的单线。
class What_I_have
{
public void TableAdapterFiller(TSNKF30_STRUCT_ELMNTTableAdapter TableAdapter,
dsGrids.TSNKF30_STRUCT_ELMNTDataTable DataTable)
{
try
{
TableAdapter.Fill(DataTable);
}
catch (SystemException sex)
{
GenericExcHandler(sex, DataTable.TableName);
}
}
public void TableAdapterFiller(TSNKF52_CGFM_USERSTableAdapter TableAdapter,
dsGrids.TSNKF52_CGFM_USERSDataTable DataTable)
{
try
{
TableAdapter.Fill(DataTable);
}
catch (SystemException sex)
{
GenericExcHandler(sex, DataTable.TableName);
}
}
}
这是我想要做的事情的想法('HowToTypeThis'这个类型提出了我的困境):
class What_Id_like
{
public void TableAdapterFiller(TSNKF30_STRUCT_ELMNTTableAdapter TableAdapter,
dsGrids.TSNKF30_STRUCT_ELMNTDataTable DataTable)
{
IsolatedMethod(TableAdapter, DataTable);
}
public void TableAdapterFiller(TSNKF52_CGFM_USERSTableAdapter TableAdapter,
dsGrids.TSNKF52_CGFM_USERSDataTable DataTable)
{
IsolatedMethod(TableAdapter, DataTable);
}
void IsolatedMethod(HowToTypeThis tabAdapter,
HowToTypeThis DataTable)
{
try
{
TableAdapter.Fill(DataTable);
}
catch (SystemException sex)
{
GenericExcHandler(sex, DataTable.TableName);
}
}
}
答案 0 :(得分:3)
添加界面:
public interface ITableAdapter
{
Fill(IdataTable dt);
}
public interface IDataTable { }
更新课程
public class TSNKF30_STRUCT_ELMNTTableAdapter : ITableAdapter {}
public class TSNKF30_STRUCT_ELMNTDataTable : IDataTable {}
编写扩展方法
public static class TableAdapterExtensions
{
public static void FillWith(this ITableAdapter adapter, IDataTable dataTable)
{
try
{
adapter.Fill(dataTable);
}
catch(SystemException sex)
{
GenericExcHandler(sex, DataTable.TableName);
}
}
}
然后用法:
var ta = new TSNKF30_STRUCT_ELMNTTableAdapter();
var dt = new dsGrids.TSNKF30_STRUCT_ELMNTDataTable();
ta.FillWith(dt);