2

I have the following code to generalize many very similar objects' initialization. I've generalized the c# code (as shown below). Anyone know a better way? This isn't too bad, but still involves some copy/paste, which I'd like to avoid.

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;
}
Quantic
  • 1,779
  • 19
  • 30
HumbleWebDev
  • 555
  • 4
  • 20
  • 1
    Why not just return the `DataAdapter` since you can get all of the commands from it? – D Stanley Jan 06 '17 at 21:18
  • Wouldn't the generated sql commands only be the most basic sql command? Like if you need more than just the obvious update/insert/delete, the generated ones are kinda worthless? – HumbleWebDev Jan 06 '17 at 21:20
  • I'm not talking about the generated ones - I'm talking about the ones you create (e.g. `dataAdapter.InsertCommand = insertCommand;` You can just return the `DataAdapter` you create and get the commands that you assigned to it. – D Stanley Jan 06 '17 at 21:22
  • Oh I seee now XD that makes a lot sense rotflmao – HumbleWebDev Jan 06 '17 at 21:31
  • Though, I need to keep aliases to atleast the select commands... – HumbleWebDev Jan 06 '17 at 21:33
  • So later I can call competitorSelectCommand.Parameters["paramName"].Value = SomeControl.Text; – HumbleWebDev Jan 06 '17 at 21:34
  • no wait, no i don't. just use the . operator XD omg *facepalms* – HumbleWebDev Jan 06 '17 at 21:36
  • Rather than using a `Tuple` with five components, your code would probably be clearer if you created a class with five properties and used that. After that, you will also be able to replace `customerSelectCommand` and its four sister properties with just one property. Do the same for `competitorSelectCommand` and its sisters. – Tanner Swett Jan 07 '17 at 04:04

1 Answers1

2

I would create an extension method like this:

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();
    }
}

Then use it like this:

var adapter = new SqlDataAdapter();
adapter.InitializeCommandsFor("Customer");
// Now the select, update, delete and insert commands are in your adapter

I also changed the name of your method to use Camel Case notation as per the .NET conventions.

Make sure, if you create the extension class in another namespace, to import that namespace by using statement.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64