2
많은 유사한 객체의 초기화를 일반화하기 위해 다음 코드를 사용합니다. 나는 C# 코드를 일반화했다 (아래 그림 참조). 누구든지 더 좋은 방법을 알고 있습니까? 이것은 나쁘지는 않지만 여전히 피하고 싶은 일부 복사/붙여 넣기가 필요합니다. 이런 식으로 사용 그런개체 초기화를 일반화하는 더 나은 방법은 무엇입니까?
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
은 또한 낙타 케이스 표기법을 사용하도록 메소드의 이름을 변경
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;
}
'DataAdapter'는 모든 명령을 얻을 수 있기 때문에 왜 반환하지 않는 것이 좋을까요? –
생성 된 sql 명령은 가장 기본적인 sql 명령이 아닙니까? 명백한 업데이트/삽입/삭제 이상의 것을 필요로하는 것처럼 생성 된 것들은 쓸데없는 것입니까? – HumbleWebDev
나는 생성 된 것들에 관해서 이야기하는 것이 아니다. 나는 당신이 만든 것들에 대해 이야기하고있다. (예,'dataAdapter.InsertCommand = insertCommand;'당신이 만든'DataAdapter'를 반환 할 수 있고 당신이 지정한 명령을 얻을 수있다. –