BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Algorithms and Data Structures
.NET 3.5+

An Extensible Appointment Scheduling Library

Scheduling software can be used to plan future appointments, including those appointments that repeat on a regular basis. This article describes a library with four types of scheduling rule and the potential for additional rules to be incorporated.

Creating the Project

To begin constructing the scheduling software, create a new code library project named, "Scheduler".

Period Class

There are two places in the library where we need to deal with a date range. When generating appointments we specify the start and end dates for the period required. When creating recurring schedules, we can specify two dates between which the schedule is active and outside of which the schedule produces no appointments. When we deal with such date ranges we use an instance of the Period class.

The Period class holds a start and end date that are set using the constructor and held in read-only DateTime properties. The constructor processes these dates to remove any time element and to ensure that the start date is on or before the end date.

public class Period
{
    public DateTime Start { get; private set; }
        
    public DateTime End { get; private set; }

    public Period(DateTime start, DateTime end)
    {
        Start = start.Date;
        End = end.Date;

        if (Start > End)
        {
            throw new ArgumentException("The start date may not be after the end date.");
        }
    }
}

Schedule Class

The Schedule class is the base class for all types that implement scheduling rules. It is abstract, so cannot be instantiated in its own right. However, it defines two concrete properties. TimeOfDay is set to the time of the appointment; each appointment may only occur once per day. Name specifies a name for the appointment. This would be useful if using the scheduling library to create calendars. You could add extra properties according to your purpose. For example, you may add properties for a command to execute if you use the schedulers to launch programs in a similar manner to the Windows Task Scheduler.

The OccursOnDate method is abstract and must be implemented by subclasses. When passed a date, the schedulers must determine if an appointment falls on that date. If one does, the method should return true. If not, the method returns false.

public abstract class Schedule
{
    public TimeSpan TimeOfDay { get; set; }

    public string Name { get; set; }

    public abstract bool OccursOnDate(DateTime date);
}

The Schedule class is the first point of extensibility. If you wish to add a new scheduling rule class that does not require a scheduling period, as we will see shortly, it should be derived from Schedule.

SingleSchedule Class

The simplest scheduling class allows for a single appointment at a given date and time. The SingleSchedule class inherits from Schedule and adds one property that holds the date of the appointment. This date will be combined with the time in the inherited TimeOfDay property when a calendar is generated.

The rule for the OccursOnDate method is very simple. Only when the date passed to the parameter matches that of the Date property does the method return true.

public class SingleSchedule : Schedule
{
    public DateTime Date { get; set; }

    public override bool OccursOnDate(DateTime date)
    {
        return Date.Date == date;
    }
}

Appointment Class

The Appointment class holds information about generated appointments and does not add any functionality. All that's required is the Time property, which actually holds a date and time, and the Name property.

public class Appointment
{
    public DateTime Time { get; set; }

    public string Name { get; set; }
}
29 April 2012