I am working in C# to write a scheduling application for operations in a factory. I have a custom class Workday
, with properties to represent a Workday
for the operations in the shop. I also have a class Timeline
that contains a dictionary of DateTime
s as keys (using DateTime.Date
to make sure we don't have two Workday
s on the same date) and a Workday
with the same Date
as Key.Date
as the value. When writing getters/setters for the Dictionary
in the Timeline
class, is it better to allow for overwriting a Workday
, or is it preferred to clear the properties of the Workday
and repopulate those values with new data?
public class Workday
{
private Dictionary<ShopOperation, List<BOMProject>> OperationProjectPairs = new Dictionary<ShopOperation, List<BOMProject>>();
public DateTime DayStart { get; set; }
public DateTime DayEnd { get; set; }
public void SetProjects(ShopOperation key, List<BOMProject> value)
{
if (OperationProjectPairs.ContainsKey(key))
{
OperationProjectPairs[key] = value;
}
else
{
OperationProjectPairs.Add(key, value);
}
}
public List<BOMProject> GetProjects(ShopOperation key)
{
List<BOMProject> result = null;
if (OperationProjectPairs.ContainsKey(key))
{
result = OperationProjectPairs[key];
}
return result;
}
}
Timeline
with getter that allows for full overwrite:
public class Timeline
{
private Dictionary<DateTime, Workday> ScheduledDays = new Dictionary<DateTime, Workday>();
public Workday GetWorkday( DateTime date )
{
Workday result = null;
foreach( KeyValuePair<DateTime, Workday> pair in ScheduledDays)
{
if( pair.Key.Date == date.Date)
{
result = pair.Value;
}
}
return result;
}
public void SetWorkday( DateTime key, Workday value)
{
bool scheduled = false;
foreach( KeyValuePair<DateTime, Workday> pair in ScheduledDays)
{
if( pair.Key.Date == value.DayStart.Date)
{
ScheduledDays[key] = value;
scheduled = true;
break;
}
}
if(!scheduled)
{
ScheduledDays.Add(key, value);
}
}
}
Timeline
that will throw an exception if you try to overwrite a Workday
:
public class Timeline
{
private Dictionary<DateTime, Workday> ScheduledDays = new Dictionary<DateTime, Workday>();
public Workday GetWorkday( DateTime date )
{
Workday result = null;
foreach( KeyValuePair<DateTime, Workday> pair in ScheduledDays)
{
if( pair.Key.Date == date.Date)
{
result = pair.Value;
}
}
return result;
}
public void SetWorkday( DateTime key, Workday value) Throws WorkdayOverwriteException
{
foreach( KeyValuePair<DateTime, Workday> pair in ScheduledDays)
{
if( pair.Key.Date == value.DayStart.Date)
{
throw new WorkdayOverwriteException();
}
}
ScheduledDays.Add(key, value);
}
}