Database Reference
In-Depth Information
This model represents reservations for train travel. Each reservation is for a particular scheduled train departure.
To create the model and prepare the application for unit testing, do the following:
1.
Create an empty solution. Right-click the solution in the Solution Explorer, and
select Add New Project. Add a new Class Library project. Name this new project
TrainReservation .
2.
Right-click the TrainReservation project, and select Add New Item. Add a new ADO.
NET Entity Data Model. Import the Train, Schedule, and Reservation tables. The resulting
model should look like the one in Figure 8-9 .
3.
Add the IValidate interface and ChangeAction enum in Listing 8-11 to the project.
Listing 8-11. The IValidate Interface
public enum ChangeAction
{
Insert,
Update,
Delete
}
interface IValidate
{
void Validate(ChangeAction action);
}
4.
Add the code in Listing 8-12 to the project. This code adds the validation code (the
implementation of IValidate) to the Reservation and Schedule classes.
Listing 8-12. Implementation of the IValidate Interface for the Reservation and Schedule Classes
public partial class Reservation : IValidate
{
public void Validate(ChangeAction action)
{
if (action == ChangeAction.Insert)
{
if (Schedule.Reservations.Count(r =>
r.ReservationId != ReservationId &&
r.Passenger == this.Passenger) > 0)
throw new InvalidOperationException(
"Reservation for the passenger already exists");
}
}
}
public partial class Schedule : IValidate
{
public void Validate(ChangeAction action)
{
if (action == ChangeAction.Insert)
{
if (ArrivalDate < DepartureDate)
 
Search WWH ::




Custom Search