I am often tasked with processing a list of tasks. Usually, this requires me to go fetch something programmatically and store it somewhere (database, file share, ftp, etc). Then I have to process the text file, xml, sql result set, or whatever it is, and store my results.
For example:
I have a database that details a list of a bunch of networks that may or may not be connected to my network. If not, I have to use Windows RAS to connect to a server on the network. Some of the networks may have more than a single computer. I have to pull a configuration file from every system (whether the network has one or many), and store that in a database. Then, I have to parse out specific information from those files and store that in the database. I also need to keep track of when a system has been checked and if there are any errors.
I have the database abstraction down, it is nice neat and organized. My database is ready for the data, all errors, checks for specific systems, errors, etc.
The rest of the application, however, is a different story. It always seems to be wrapped up in one loop in the main function, with some random classes (ie ConfigurationFile class, ConnectToNetwork class, etc) to hold functions that do tasks specific to my application ( really, my question is probably how to organize those classes).
Here's and example:
static void main()
{
var networks = NetworkLocationsRepository.GetAll();
foreach(var network in networks)
{
ConfigurationFile file = null;
if(!network.IsConnected)
{
ConnectToNetwork(network);
}
foreach(var system in network.Systems)
{
file = GetConfigFile(system);
ConfigurationFileRepository.Add(file);
var configData = ParseConfiguration(file);
ConfigurationFileRepository.UpdateData(file, configData);
}
}
}
Edit
In response to the comments, I'm not so concerned with the loop (even though that's what my title focuses on). I know that because I have a collection of items to iterate, in some shape or form, I'm going to have a loop. Really, I'm more concerned with all of the stuff that gets put inside the loop. This example is fairly basic, but say I have 10 different tasks to complete on every system? Surely using a bunch of if statements to determine if tasks should be completed stuffed into a single foreach loop is not the best approach?