泛化对象初始化的更好方法?
Better way to Generalize Object Initialization?
我有以下代码来概括许多非常相似的对象的初始化。我已经概括了 c# 代码(如下所示)。有人知道更好的方法吗?这还算不错,但仍然涉及一些 copy/paste,我想避免这种情况。
private Tuple<SqlCommand, SqlCommand, SqlCommand, SqlCommand, SqlDataAdapter> initializeCommandsFor (string type) {
SqlCommand selectCommand;
SqlCommand insertCommand;
SqlCommand updateCommand;
SqlCommand deleteCommand;
SqlDataAdapter dataAdapter;
selectCommand = new SqlCommand("Get" + type + "Data", cntn);
selectCommand.CommandType = CommandType.StoredProcedure;
insertCommand = new SqlCommand("Insert" + type, cntn);
insertCommand.CommandType = CommandType.StoredProcedure;
updateCommand = new SqlCommand("Update" + type, cntn);
updateCommand.CommandType = CommandType.StoredProcedure;
deleteCommand = new SqlCommand("Delete" + type, cntn);
deleteCommand.CommandType = CommandType.StoredProcedure;
cntn.Open();
SqlCommandBuilder.DeriveParameters(selectCommand);
cntn.Close();
dataAdapter = new SqlDataAdapter(selectCommand);
dataAdapter.InsertCommand = insertCommand;
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.DeleteCommand = deleteCommand;
return Tuple.Create(selectCommand, insertCommand, updateCommand, deleteCommand, dataAdapter);
}
private void customerCommands () {
var commands = initializeCommandsFor("Customer");
customerSelectCommand = commands.Item1;
customerInsertCommand = commands.Item2;
customerUpdateCommand = commands.Item3;
customerDeleteCommand = commands.Item4;
customerDataAdapter = commands.Item5;
}
private void competitorCommands () {
var commands = initializeCommandsFor("Competitor");
competitorSelectCommand = commands.Item1;
competitorInsertCommand = commands.Item2;
competitorUpdateCommand = commands.Item3;
competitorDeleteCommand = commands.Item4;
competitorDataAdapter = commands.Item5;
}
我会像这样创建一个扩展方法:
public static class SqlDataAdapterExtension
{
public static void InitializeCommandsFor(this SqlDataAdapter adapter, string type)
{
SqlCommand selectCommand;
SqlCommand insertCommand;
SqlCommand updateCommand;
SqlCommand deleteCommand;
SqlDataAdapter dataAdapter;
adapter.SelectCommand = new SqlCommand("Get" + type + "Data", cntn);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.InsertCommand = new SqlCommand("Insert" + type, cntn);
adapter.InsertCommand.CommandType = CommandType.StoredProcedure;
adapter.UpdateCommand = new SqlCommand("Update" + type, cntn);
adapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
adapter.DeleteCommand = new SqlCommand("Delete" + type, cntn);
adapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
cntn.Open();
SqlCommandBuilder.DeriveParameters(adapter.SelectCommand);
cntn.Close();
}
}
然后像这样使用它:
var adapter = new SqlDataAdapter();
adapter.InitializeCommandsFor("Customer");
// Now the select, update, delete and insert commands are in your adapter
我还根据 .NET 约定将您的方法名称更改为使用驼峰命名法。
如果您在另一个命名空间中创建扩展 class,请确保通过 using
语句导入该命名空间。
我有以下代码来概括许多非常相似的对象的初始化。我已经概括了 c# 代码(如下所示)。有人知道更好的方法吗?这还算不错,但仍然涉及一些 copy/paste,我想避免这种情况。
private Tuple<SqlCommand, SqlCommand, SqlCommand, SqlCommand, SqlDataAdapter> initializeCommandsFor (string type) {
SqlCommand selectCommand;
SqlCommand insertCommand;
SqlCommand updateCommand;
SqlCommand deleteCommand;
SqlDataAdapter dataAdapter;
selectCommand = new SqlCommand("Get" + type + "Data", cntn);
selectCommand.CommandType = CommandType.StoredProcedure;
insertCommand = new SqlCommand("Insert" + type, cntn);
insertCommand.CommandType = CommandType.StoredProcedure;
updateCommand = new SqlCommand("Update" + type, cntn);
updateCommand.CommandType = CommandType.StoredProcedure;
deleteCommand = new SqlCommand("Delete" + type, cntn);
deleteCommand.CommandType = CommandType.StoredProcedure;
cntn.Open();
SqlCommandBuilder.DeriveParameters(selectCommand);
cntn.Close();
dataAdapter = new SqlDataAdapter(selectCommand);
dataAdapter.InsertCommand = insertCommand;
dataAdapter.UpdateCommand = updateCommand;
dataAdapter.DeleteCommand = deleteCommand;
return Tuple.Create(selectCommand, insertCommand, updateCommand, deleteCommand, dataAdapter);
}
private void customerCommands () {
var commands = initializeCommandsFor("Customer");
customerSelectCommand = commands.Item1;
customerInsertCommand = commands.Item2;
customerUpdateCommand = commands.Item3;
customerDeleteCommand = commands.Item4;
customerDataAdapter = commands.Item5;
}
private void competitorCommands () {
var commands = initializeCommandsFor("Competitor");
competitorSelectCommand = commands.Item1;
competitorInsertCommand = commands.Item2;
competitorUpdateCommand = commands.Item3;
competitorDeleteCommand = commands.Item4;
competitorDataAdapter = commands.Item5;
}
我会像这样创建一个扩展方法:
public static class SqlDataAdapterExtension
{
public static void InitializeCommandsFor(this SqlDataAdapter adapter, string type)
{
SqlCommand selectCommand;
SqlCommand insertCommand;
SqlCommand updateCommand;
SqlCommand deleteCommand;
SqlDataAdapter dataAdapter;
adapter.SelectCommand = new SqlCommand("Get" + type + "Data", cntn);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.InsertCommand = new SqlCommand("Insert" + type, cntn);
adapter.InsertCommand.CommandType = CommandType.StoredProcedure;
adapter.UpdateCommand = new SqlCommand("Update" + type, cntn);
adapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
adapter.DeleteCommand = new SqlCommand("Delete" + type, cntn);
adapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
cntn.Open();
SqlCommandBuilder.DeriveParameters(adapter.SelectCommand);
cntn.Close();
}
}
然后像这样使用它:
var adapter = new SqlDataAdapter();
adapter.InitializeCommandsFor("Customer");
// Now the select, update, delete and insert commands are in your adapter
我还根据 .NET 约定将您的方法名称更改为使用驼峰命名法。
如果您在另一个命名空间中创建扩展 class,请确保通过 using
语句导入该命名空间。