Between clarity and complexity, which to choose?

  • Thread starter Thread starter Robert in SF
  • Start date Start date
R

Robert in SF

Guest
Suppose you want to load a whole bunch of small lookup tables into your dataset, and if you use each table's TableAdapter, it will take forever, so you set up something like this.

public static class DataHelper
{
public struct DBentry
{
public string NetTable { get; set; }
public string SQLTable { get; set; }
public string SQLField { get; set; }
public DBentry(string netTable, string sqlTable, string sqlField)
{
NetTable = netTable;
SQLTable = sqlTable;
SQLField = sqlField;
}
}

public static void LoadLookups(DataSet ds, List<DBentry> tableList)
{
var connection = new OleDbConnection(Properties.Settings.Default.LSOfficeConnectionString);
connection.Open();
var command = new OleDbCommand();
command.Connection = connection;
foreach (DBentry entry in tableList)
{
command.CommandText = $"SELECT ID, {entry.SQLField} FROM {entry.SQLTable};";
ds.Tables[entry.NetTable].Load(command.ExecuteReader());
}
command.Dispose();
connection.Close();
connection.Dispose();
}
}

which you then use with code like this (dsRequests1 is a DataSet).

List<DataHelper.DBentry> DBList = new List<DataHelper.DBentry>();
DBList.Add(new DataHelper.DBentry("Requesters", "Requesters", "RName"));
DBList.Add(new DataHelper.DBentry("NeededBy", "NeededBy", "NName"));
DBList.Add(new DataHelper.DBentry("EnteringFulfillers", "Fulfillers", "FName"));
DBList.Add(new DataHelper.DBentry("AssigningFulfillers", "Fulfillers", "FName"));
DBList.Add(new DataHelper.DBentry("RequestFulfillers", "Fulfillers", "FName"));
DBList.Add(new DataHelper.DBentry("Tasks", "Tasks", "TName"));
DBList.Add(new DataHelper.DBentry("RequestStatus", "RequestStatus", "SName"));
DataHelper.LoadLookups(dsRequests1, DBList);

You marvel at how clear this code is, but then your co-worker says he can make the code simpler and just as clear. Like this.

public static void LoadLookups2(DataSet ds, string [,] tableList)
{
var connection = new OleDbConnection(Properties.Settings.Default.LSOfficeConnectionString);
connection.Open();
var command = new OleDbCommand();
command.Connection = connection;
for (int ndx = 0; ndx < tableList.Length; ndx++)
{
command.CommandText = $"SELECT ID, {tableList[ndx, 2]} FROM {tableList[ndx, 1]};";
ds.Tables[tableList[ndx, 0]].Load(command.ExecuteReader());
}
command.Dispose();
connection.Close();
connection.Dispose();
}

which you then use with the much simpler

string[,] entries = new string[,] {
{ "Requesters", "Requesters", "RName" },
{ "NeededBy", "NeededBy", "NName" },
{ "EnteringFulfillers", "Fulfillers", "FName" },
{ "AssigningFulfillers", "Fulfillers", "FName" },
{ "RequestFulfillers", "Fulfillers", "FName" },
{ "Tasks", "Tasks", "TName" },
{ "RequestStatus", "RequestStatus", "SName" },
};
DataHelper.LoadLookups(dsRequests1, entries);

Now, it's true that the code is somewhat less readable with array index notation, but to the caller of the code, the setup is undeniably simpler, plus the caller doesn't need to know what's inside the black box of DataHelper. The caller just knows that, if it supplies a list of table entries, its target DataSet will be loaded with those tables.

Which would you go for, and why would you pick that method over the other?

Continue reading...
 
Back
Top