The code below describes my design issue in detail. I have a task table in a database that can have different types of recurrence patterns. The task table has columns for each of the possible recurrence pattern fields. However, I want the Task object to create the proper pattern based on which pattern is in the db. The code below will do accomplish this, but then the problem is the calling code will always have to check the type of Recurrence being returned before taking any action.
e.g.
var t = new Task();
var pattern = t.Recurrance;
The calling code has no idea what type of recurrance is being created?
What is a better way to model this?
class Task
{
private int recurrenceType = 0; //pulled from the db
public Task()
{
//determine recurrence type from database
switch (recurrenceType)
{
case 0:
Recurrance = new RecurrenceDaily();
break;
case 1:
Recurrance = new RecurrenceMonthly();
break;
case 2:
Recurrance = new RecurrenceWeekly();
break;
}
}
public RecurrenceBase Recurrance { get; set;}
}
abstract class RecurrenceBase
{
public int Frequency { get; set; }
}
class RecurrenceDaily : RecurrenceBase
{
public bool Weekends { get; set; }
}
class RecurrenceWeekly : RecurrenceBase
{
public DaysOfWeekFlagsEnum DaysOfWeek { get; set; }
}
class RecurrenceMonthly : RecurrenceBase
{
public byte DayOfMonth { get; set; }
public WeekEnum Week { get; set; }
public DayOfWeekEnum DayOfWeek { get; set; }
}