Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP.NET 2.0 Instant Results

.pdf
Скачиваний:
67
Добавлен:
17.08.2013
Размер:
11.03 Mб
Скачать

Appointment Booking System

Figure 10-6

The following table describes the five public properties of the BookingObject class:

Property

Type

Description

 

 

 

AvailableOnWeekDays

Weekdays

This property is of type Weekdays, which is an

 

 

enumeration. This property is used to store multiple

 

 

days on which the booking object is available.

EndTime

Integer

The last hour that the booking object is available on

 

 

a day. Because the application books appointments

 

 

for a full hour, the maximum value for EndTime is

 

 

23, which means the booking object is available

 

 

until midnight. (The last appointment then starts at

 

 

11 o’clock at night and lasts until midnight.)

Id

Integer

The unique ID of the BookingObject in the

 

 

database.

StartTime

Integer

The first hour of the day that the booking object

 

 

becomes available; for example, 9 for 9 a.m.

Title

String

The description for the BookingObject, such as

 

 

Conference Room 1 — East Wing.

 

 

 

Next up is the BookingObjectManager class.

BookingObjectManager

This class is responsible for getting booking objects in and out of the database by interacting with the data access layer. It’s also used to return a list with available working days from the data access layer through its GetWorkingDays method. The class has the methods shown in Figure 10-7.

317

Chapter 10

Figure 10-7

Because the class exposes shared methods exclusively, its constructor has been hidden by marking it as Private. This prevents calling code from instantiating the BookingObjectManager directly. Besides the constructor, the class exposes four shared and public methods that all call methods with the same name in the BookingObjectManagerDB class in the data access layer:

Method

Return Type

Description

 

 

 

Public Shared Function

BookingObject

Returns a single BookingObject instance by

GetBookingObject (ByVal

 

its ID.

id As Integer)

 

 

Public Shared Function

DataSet

Returns a list of available booking object

GetBookingObjectList ()

 

records as a DataSet.

Public Shared Function

DataSet

Returns a list with the available Working

GetWorkingDays()

 

Days.

Public Shared Sub

n/a

Saves a new or an existing booking object in

SaveBookingObject

 

the database.

(ByVal myBookingObject

 

 

As BookingObject)

 

 

 

 

 

With the BookingObject and BookingObjectManager classes done, the next class is the Appointment class, which is used to make an actual appointment with a booking object.

Appointment

The Appointment class, shown in Figure 10-8, represents an appointment that has been made in the system. It exposes only public properties that are used to track when the appointment takes place, who made it, and for what booking object.

318

Appointment Booking System

Figure 10-8

To help you understand what these properties are used for, the following table lists them all and describes their purpose:

Property

Type

Description

 

 

 

BookingObjectId

Integer

The ID of the booking object in the database that this

 

 

appointment was booked against.

Comments

String

Stores the comment for an appointment. When a user

 

 

makes an appointment, she has the opportunity to add

 

 

a comment. Whether this comment is required depends

 

 

on the application’s settings.

EndDate

DateTime

The date and time the appointment ends.

Id

Integer

The unique ID of the appointment in the database.

StartDate

DateTime

The date and time the appointment starts.

UserEmailAddress

String

Holds the e-mail address of the user in the application

 

 

and is retrieved through the Membership services in

 

 

ASP.NET.

UserName

String

Holds the name of the user in the application and is

 

 

retrieved through the Membership services in ASP.NET.

319

Chapter 10

Similar to the BookingObject class, the Appointment class also has an accompanying Manager class, the AppointmentManager, discussed next.

AppointmentManager

The AppointmentManager class, depicted in Figure 10-9, has useful methods to get and create appointments. It can also determine whether a new appointment overlaps with an existing one, and it is capable of retrieving appointment information from the database to feed the Availability Checker.

Figure 10-9

Just as with the BookingObjectManager, the constructor for the AppointmentManager (the New method in Figure 10-9) has been hidden by marking it as Private. This prevents calling code from instantiating objects from this class. You never require an instance of these classes, because they expose shared methods that work only on a class and not on an instance of that class.

Besides the constructor, the AppointmentManager has five public and shared methods that all call methods with the same name in the AppointmentManagerDB class. These methods are discussed in the following table:

Method

Return Type

Description

 

 

 

Public Shared Function

Boolean

Checks whether the requested appointment

CheckAppointment

 

overlaps with an existing one. It returns

(ByVal myAppointment

 

True when the appointment passed in can

As Appointment)

 

be made, or False when it overlaps.

Public Shared Function

Boolean

Creates an appointment in the database. It

CreateAppointment

 

returns True when the appointment is

(ByVal myAppointment

 

successfully made, or False when it could

As Appointment)

 

not be booked.

Public Shared Function

Appointment

Retrieves a single instance of an appointment

GetAppointment (ByVal

 

from the database by its ID. This method is

id As Integer)

 

used in the reporting pages in the Manage-

 

 

ment section of the site.

Public Shared Function

DataSet

Retrieves a list of all the appointments for a

GetAppointmentList

 

specific date from the database. This method

(ByVal selectedDate

 

is used in the reporting pages in the

As DateTime)

 

Management section of the site.

320

 

 

 

Appointment Booking System

 

 

 

 

 

 

 

 

 

Method

Return Type

Description

 

 

 

 

 

Public Shared Function

DataSet

Returns a DataSet with two DataTables,

 

GetTimeSheet (ByVal

 

holding booking objects and appointments.

 

selectedDate As DateTime)

 

This DataSet is used generate the chart for

 

 

 

the Availability Checker.

The final object in the business layer that you need to look at is not a class, but an enumeration. This enumeration, called Weekdays, is discussed next.

Weekdays

Although there is already good support for working with days of the week in .NET, the Appointment Booking System features a separate enumeration that lists all of the available weekdays. This enumeration allows you to store multiple selected weekdays in a single variable. The BookingObject uses this enumeration to indicate on which day the object can be booked. Instead of this enumeration, the BookingObject could expose seven Boolean properties, such as AvailableOnMonday, AvailableOnTuesday, and so forth, but that makes the class look a bit cluttered. Using this Weekdays enumeration, displayed in Figure 10-10, you can store the availability for multiple days in a single variable.

Figure 10-10

With this simple enumeration, you can store, for example, Friday and Wednesday in a variable of type Weekdays with the following code:

Dim myWeekdays As Weekdays = Weekdays.Wednesday Or Weekdays.Friday

Later, you can use similar code to determine whether a certain day has been stored in that variable:

If myWeekdays And Weekdays.Friday Then

Friday was selected and stored in myWeekdays

Else

Friday was NOT selected

End If

Because the Appointment Booking System is quite data-centric, it should come as no surprise it has its own database and data access layer. In the next section, the two classes in the data access layer are discussed. Once you understand how these classes work, you get a good look at the tables and stored procedures that make up the database.

321

Chapter 10

The Data Access Layer

Because the BookingObject and Appointment classes have no behavior themselves but are managed by their respective Manager classes instead, they also have no companion class in the data access layer. The only classes that interact with the stored procedures in the database directly are the

BookingObjectManagerDB and the AppointmentDB classes.

BookingObjectManagerDB

The BookingObjectManagerDB class (see Figure 10-11) exposes the exact same four public and shared methods and the private constructor as the BookingObjectManager class. Of course this isn’t a coincidence, because each method in the business layer forwards the call to a method in the data access layer.

Figure 10-11

Because the methods are identical as those in the BookingObjectManager in terms of signature, return type, and functionality, they aren’t described here again. Refer to the description of the BookingObject class earlier in this chapter for a full description of the four methods. The only difference between the methods in the business and data access layer is, of course, their implementation. The methods in the business layer forward their calls to methods in the data access layer. Those methods in turn perform the real work and communicate with the database. You see how this works in the section “Code and Code Explanation.”

AppointmentManagerDB

The AppointmentManagerDB class (see Figure 10-12) is responsible for getting appointments, lists of appointments, and time sheet information from the database. It’s also capable of checking and creating appointments.

Figure 10-12

Each of the methods in this class has the same signature as those in the AppointmentManager class in the business layer, so refer to that section for more detail about their signatures and description.

322

Appointment Booking System

In addition to these classes, the data access layer also consists of the database itself, including the stored procedures used to access the data. The following section describes the data model of the Appointment Booking System and describes each of the four tables.

The Data Model

The database for the Appointment Booking System contains three main tables and one junction table. Both the BookingObject and the Appointment classes you saw in the design of the business layer have their own table in the database.

There is also a table called WorkingDay that stores the available working days for the application. Don’t confuse this table with the Weekdays enumeration. This enumeration always defines all seven days of the week, whereas the WorkingDay table stores only the actual days of the week that are appropriate for your booking objects. If your booking objects are available only during the week, you could remove Saturday and Sunday from this table.

The final table in the database is called BookingObjectWorkingDay. This junction table relates a certain booking object to one or more working days, as you can see in Figure 10-13. This allows you to have a different availability for different booking objects.

Figure 10-13

The BookingObject and Appointment tables require a bit more explanation, so they are described in more detail in the following two tables.

BookingObject

Column Name

Data Type

Description

 

 

 

Id

int

Stores the unique ID of each booking object.

Title

nvarchar (100)

Stores the title of a booking object such as Conference

 

 

Room 6.

StartTime

datetime

Stores the first available time a booking object is available

 

 

during the day. Although the column type is datetime,

 

 

only the time portion of the datetime is used.

EndTime

datetime

Stores the last available time a booking object is available

 

 

during the day. Although the column type is datetime,

 

 

only the time portion of the datetime is used.

 

 

 

323

Chapter 10

This BookingObject table is the data store for the BookingObject class. Four of the properties of that class have their own column in this table. The AvailableOnWeekdays property is not stored in that table, but in the junction table called BookingObjectWorkingDay.

Similar to this, the Appointment class has its own table, also called Appointment.

Appointment

 

 

 

 

 

 

 

Column Name

Data Type

Description

 

 

 

 

 

Id

int

Stores the unique ID of the appointment.

 

UserName

nvarchar (256)

Stores the name of the user that made the appointment.

 

UserEmailAddress

nvarchar (256)

Stores the e-mail address of the user that made the

 

 

 

appointment.

 

StartDate

datetime

Stores the start date and time of the appointment.

 

EndDate

datetime

Stores the end date and time of the appointment.

 

Comments

nvarchar (max)

Stores the comments that a user may have added to the

 

 

 

appointment request.

 

BookingObjectId

int

Stores the ID of the booking object that this appointment

 

 

 

was booked against.

All of the interaction with the database is done through stored procedures. Some of the procedures are pretty straightforward and require no explanation. The others that are a bit more complex are discussed in detail when the inner workings of the Appointment Booking System are discussed.

Helper Classes

In addition to the classes and enumeration defined in the business and data access layer, the Appointment Booking System has two more classes: an AppConfiguration class that exposes configuration properties used throughout the application and a Helpers class that supplies a useful helper method.

AppConfiguration

The AppConfiguration class (see Figure 10-14) is essentially a wrapper around some of the configuration keys in the Web.config file. Although ASP.NET 2.0 provides a convenient way to bind keys from the Web.config file to controls in the markup of a page using the new expression syntax you see later, you still need to write some code to accomplish the same thing in code-behind files or in code in the App_Code folder. To avoid repeating this code many times over, the AppConfiguration class provides convenient access to the keys in the Web.config file through read-only and shared properties.

The BookingObjectNamePlural and BookingObjectNameSingular properties expose the userfriendly descriptions of the booking object. These properties are used to customize the user interface in the public and Management section of the web site.

324

Appointment Booking System

Figure 10-14

The RequireCommentsInRequest property determines whether a user has to enter a comment in the appointment wizard. You see how this works later.

The ConnectionString property is used by all methods in the two classes in the data access layer.

Helpers

The Helpers class, shown in Figure 10-15, provides one shared method — GetCurrentServerRoot — that returns the full root URL of the current application.

Figure 10-15

Instead of hard-coding the application URL, like http://www.yoursite.com somewhere in your code, this method determines the application’s address at run time. It is used to customize the opt-in e-mail message in the Sign Up page that you see later.

Code and Code Explanation

Most of the pages in the root of the application and the Management folder are part of the public front end and Management sections of the site and are discussed in full detail later. There are, however, a few files and folders that you need to look at first.

Web.config

The Web.config file contains three <appSettings> keys and one connection string that map directly to the four properties of the AppConfiguration class you just saw. Most of the other settings in this file are either the default settings or have been discussed in previous chapters, so they aren’t covered here anymore. The only exception are the three <location> nodes at the bottom of the file. These three nodes block access to the Management folder and the files CreateAppointment.aspx and CheckAvailability.aspx for unauthenticated users.

325

Chapter 10

Global.asax

Just as in the previous three chapters, the Global.asax contains code that can send e-mail whenever an error is raised in the site. The code is identical to that in Chapter 6, so refer to that chapter if you want to know how the code works.

Default.aspx

This is the homepage for the Appointment Booking System and is based on the MasterPage.master page (discussed next).

Master Pages

The public section and the protected Management section each have their own master page. The difference between the public master (MasterPage.master) and the master page for the Management section (ManagementMaster.master) is the inclusion of the ManagementMenu user control in the latter.

Other Files and Folders

In addition to the files in the root, the Appointment Booking System uses other files and folders as well:

App_Themes: The App_Themes folder contains a single .skin file that controls the looks of each <asp:Calendar> used in the web site. The <pages> node in the Web.config file instructs the application to apply this theme to each page in the site.

Controls: This folder stores the user controls that are used throughout the site. The MainMenu and ManagementMenu controls are used to define the menus in the various pages in the site, similar to other applications in this book. The HourPicker and TimeSheet controls are very specific controls that are described in full later.

Css: The Css folder contains two CSS files that control the general structure and look of the site (Core.css) and that influence more specific elements, such as the time sheet and error messages (Styles.css).

Images: This folder contains the logo for the site, the calendar icon, and the arrow used in the main menu.

JavaScripts: This folder contains a single file called ClientScripts.js that holds a JavaScript function used in multiple pages in the site.

StaticFiles: The StaticFiles folder contains one HTML file with the contents for the opt-in e-mail. This file is used as a template for the body of the confirmation e-mail that users receive after they sign up for an account.

Now that you have seen the design of the application and database, it’s time to look at the actual functionality of the site and the code that drives it. Instead of discussing the files in the application one by one, a more usage-oriented approach is taken. You see the typical workflows of the application and the files that are involved in the process.

326