C# 概括对象初始化的更好方法?

C# 概括对象初始化的更好方法?,c#,dry,object-initialization,C#,Dry,Object Initialization,我有以下代码来概括许多非常相似的对象的初始化。我概括了c代码(如下所示)。有人知道更好的方法吗?这还不算太糟,但仍然涉及一些复制/粘贴,我希望避免这种情况 private Tuple<SqlCommand, SqlCommand, SqlCommand, SqlCommand, SqlDataAdapter> initializeCommandsFor (string type) { SqlCommand selectCommand; SqlCommand inser

我有以下代码来概括许多非常相似的对象的初始化。我概括了c代码(如下所示)。有人知道更好的方法吗?这还不算太糟,但仍然涉及一些复制/粘贴,我希望避免这种情况

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;
}
private Tuple initializeCommand(字符串类型){
SqlCommand选择命令;
sqlcommandinsertcommand;
SqlCommand-updateCommand;
SqlCommand删除命令;
SqlDataAdapter;
selectCommand=newSQLCommand(“获取”+类型+“数据”,cntn);
选择Command.CommandType=CommandType.StoredProcess;
insertCommand=newsqlcommand(“插入”+类型,cntn);
insertCommand.CommandType=CommandType.StoredProcess;
updateCommand=新的SqlCommand(“更新”+类型,cntn);
updateCommand.CommandType=CommandType.StoredProcess;
deleteCommand=新的SqlCommand(“删除”+类型,cntn);
deleteCommand.CommandType=CommandType.StoredProcess;
cntn.Open();
派生参数(selectCommand);
cntn.Close();
dataAdapter=新的SqlDataAdapter(selectCommand);
dataAdapter.InsertCommand=InsertCommand;
dataAdapter.UpdateCommand=UpdateCommand;
dataAdapter.DeleteCommand=DeleteCommand;
返回Tuple.Create(selectCommand、insertCommand、updateCommand、deleteCommand、dataAdapter);
}
私有无效客户命令(){
var commands=initializeCommandsFor(“客户”);
customerSelectCommand=commands.Item1;
customerInsertCommand=commands.Item2;
customerUpdateCommand=commands.Item3;
customerDeleteCommand=commands.Item4;
customerDataAdapter=commands.Item5;
}
私人无效竞争对手命令(){
var commands=initializeCommandsFor(“竞争对手”);
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
然后像这样使用它:

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约定,我还将方法的名称更改为使用驼峰大小写表示法


如果您在另一个名称空间中创建扩展类,请确保通过使用语句导入该名称空间。

既然可以从中获取所有命令,为什么不返回DataAdapter?生成的sql命令不是最基本的sql命令吗?比如说,如果你需要的不仅仅是显而易见的更新/插入/删除,那么生成的就没有什么价值了?我不是说生成的,我是说你创建的(例如,
dataAdapter.InsertCommand=InsertCommand;
您只需返回您创建并获取分配给它的命令的
dataAdapter
。哦,我现在看到了XD,这很有意义。不过,我需要保留别名,以至少用于选择命令。。。