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.
Scheduling tools, such as that found in Microsoft Outlook calendars and other diary software, allow future appointments to be planned and displayed in the form of a daily planner. Appointments may be one-off items, with a specific date and time, or recurring, such as those that repeat on every weekday. The algorithms that produce future schedules of this type have many uses.
In this article we will create a scheduling library using C#. The library includes the ability to create schedules and generate lists of appointments that fall between two dates. The appointment lists produced could be displayed to a user, used as the basis for automatically executing processes at specified times or for many other purposes.
We'll create four types of schedule:
- Single Appointment. These are one-off appointments that occur on a specific date at a particular time.
- Simple Recurring Schedule. These recurring appointments start on a given date and repeat every few days. The number of days between repetitions is configurable.
- Weekly Schedule. The second type of recurring schedule allows a pattern of appointments that repeats weekly. You can to specify a time for the appointments and on which of the seven days of the week they should occur. This allows options such as creating an appointment on every weekday but excluding weekends.
- Monthly Schedule. The final repeating schedule allows you to create appointments that occur on the same date of every month. When the specified day number is beyond a month end, the final date of the month is scheduled instead.
The above four options allow for many scheduling applications. However, sometimes you will require more complex rules. As you will see, the library is extensible, allowing you to create new scheduling rules by adding new classes that encapsulate them.
The scheduling library includes nine classes. The UML diagram below shows them and their relationships.
The classes in the diagram are as follows:
- CalendarGenerator. The class is used to generate lists of appointments using rules defined in one or more schedules.
- Appointment. A simple data class that holds the date, time and name of an appointment.
- Period. Represents a period of time, starting on one date and finishing on another. Periods do not include time elements.
- Schedule. An abstract base class for all scheduling rule classes, including any that you add to the library yourself. This class defines properties that specify the name of a schedule's generated appointments and the time of day at which they occur. The OccursOnDate method must be implemented by all subclasses. It is used by CalendarGenerator to determine whether an appointment occurs on a specified date. Each schedule class implements the method in a manner that defines its scheduling rules.
- SingleSchedule. Defines a one-off appointment schedule.
- RepeatingSchedule. An abstract base class for all recurring schedules. This class adds a scheduling period to the Schedule base class, allowing a repeating schedule to have a fixed start and end. Appointments are not generated for dates that are outside of this period. The class also defines a method, named "DateIsInPeriod", which can be used by any subclass to quickly identify if a given date is within the scheduling period.
- SimpleRepeatingSchedule. The first recurring scheduler creates appointments at regular intervals. The interval size is measure in days specified using the DaysBetween property.
- WeeklySchedule. This scheduling rule class creates appointments that repeat every week on given days. The list of days when appointments should be generated is defined in a generic list of DayOfWeek values.
- MonthlySchedule. The final scheduling class creates monthly appointments that occur on the date defined in the integer property, "DayOfMonth".
29 April 2012