I've been working on this code for awhile and for some reason all the if's are driving me crazy along with a bunch of repeated code. Is there a nicer more cleaner way to do this?
public Program(string[] args)
{
try
{
WriteToLogFile("Starting ImportTask");
if (args.Length == 0)
{
Import(DateTime.Now,DateTime.Now);
MarkRecordsAsDeleted();
}
else if (args.Length == 1)
{
DateTime dateToImport;
bool isValidDate = DateTime.TryParse(args[0], out dateToImport);
if (isValidDate)
{
Import(dateToImport,dateToImport);
MarkRecordsAsDeleted();
}
else
WriteToLogFile(String.Format("The Import date specified was invalid. - {0}", args[0]));
}
else if (args.Length == 2)
{
DateTime importStartDate;
bool isValidStartDate = DateTime.TryParse(args[0], out importStartDate);
DateTime importEndDate;
bool isValidEndDate = DateTime.TryParse(args[0], out importEndDate);
if (isValidStartDate && isValidEndDate)
{
if (importStartDate > importEndDate)
{
WriteToLogFile(String.Format("Invalid date range provided. Start date = {0} End date {1}",importStartDate,importEndDate));
return;
}
Import(importStartDate, importEndDate);
MarkRecordsAsDeleted();
}
else
WriteToLogFile(String.Format("The Import date specified was invalid. - {0}", args[0]));
}
else
{
WriteToLogFile("Invalid Command Line Parameters Specified");
}
}
catch (Exception ex)
{
WriteToLogFile("Error in Import Process = " + ex.StackTrace);
}
}