In my previous post in this series we covered creating a hierarchy of business objects, persisting them using XPO and then creating a test to ensure that we had wired up the persistence mechanism properly. In this post we will take our object hierarchy to the next stage, by adding in the required bahaviours, carrying out some necessary refactoring, and adding to our test project.
So let's go ahead and add the behaviours we need to our hierarchy. The required behaviours are detailed in the following use case diagram:-

As you can see, a Receptionist makes appointments and reminds a Patient of appointments whilst a PracticingEmployee updates the Patient's notes. Wait a minute? PracticingEmployee, what's that, there's not one of those in our hierarchy? No there wasn't. When we were laying out the properties of each object we shook out the hierarchy that we have. However, now when we come to add the object behaviours, we realise that whilst a Dentist and a Hygienist will update Patient notes, a receptionist will not, therefore we need a new abstract object to describe a practicing employee; we also need to add a PatientNotes object, which will maintain a collection of objects of type Note. After we add these new objects, our hierarchy will look like this:-

Now let's add the code to make an appointment:-
/// <summary>
/// Given a Patient, a PracticingEmployee and a DateTime,
/// create and return an Appointment
/// </summary>
/// <param name="patient">The Patient the Appointment is for</param>
/// <param name="practicingEmployee">The PracticingEmployee that the
/// Patient will see</param>
/// <param name="timeOfAppointment">The time of the appointment</param>
/// <param name="session">The current UnitOfWork</param>
/// <returns>An Appointment object</returns>
public Appointment MakeAppointment(Patient patient,
PracticingEmployee practicingEmployee,
DateTime timeOfAppointment,
Session session)
{
Appointment appointment = new Appointment(session);
appointment.Patient = patient;
appointment.PracticingEmployee = practicingEmployee;
appointment.TimeOfAppointment = timeOfAppointment;
return appointment;
}
and the code to remind a Patient of an Appointment:-
/// <summary>
/// Send an email reminding a Patient of their appointment
/// </summary>
/// <param name="appointment">The Appointment</param>
/// <returns></returns>
public bool RemindPatientOfAppointment(Appointment appointment)
{
string message = String.Format("Dear {0} this is an email to " +
"remind you that you have an appointment with {1} {2} on {3}" +
"at {4}",appointment.Patient.FirstName,
appointment.PracticingEmployee.FirstName,
appointment.PracticingEmployee.LastName,
appointment.TimeOfAppointment.Date.ToLongDateString(),
appointment.TimeOfAppointment.TimeOfDay.ToString());
//GS - Simulate a sucessful email send
Console.WriteLine(message);
Console.ReadLine();
return true;
}
followed by the code to update the Patient's notes:-
/// <summary>
/// Add a new note to the Patients notes
/// </summary>
/// <param name="patient">The Patient</param>
/// <param name="note">The text of the note to add</param>
/// <param name="session">The current UnitOfWork</param>
public void UpdatePatientsNotes(Patient patient, String note,
Session session)
{
Note newNote = new Note(session);
newNote.UpdateNote = note;
newNote.UpdateDate = DateTime.Now;
patient.PatientNote.Notes.Add(newNote);
}
Now we must add tests for making an appointment and updating a Patient's notes:-
[TestMethod()]
public void MakeAppointmentTest()
{
//GS - When shall we make the appointment for?
DateTime timeOfAppointment = DateTime.Now;
//GS - Ask a Receptionst to make an Appointment and persist it
using (var uow = new UnitOfWork())
{
Receptionist receptionist = new Receptionist(uow);
Patient patient = new Patient(uow);
Dentist dentist = new Dentist(uow);
receptionist.MakeAppointment(patient, dentist,
timeOfAppointment, uow);
uow.CommitChanges();
}
//GS - Verify an appointment was persisted.
using (var uow = new UnitOfWork())
{
var criteria = new BinaryOperator("TimeOfAppointment", timeOfAppointment);
var appointment = uow.FindObject<Appointment>(criteria);
Assert.IsNotNull(appointment);
}
}[TestMethod()]
public void UpdatePatientsNotesTest()
{
//GS - What text shall we add to the Patients notes?
string noteText = "The patient was seen";
//GS - Add the Note to the Patients notes and persist it
using (var uow = new UnitOfWork())
{
Dentist dentist = new Dentist(uow);
Patient patient = new Patient(uow);
PatientNote patientNote = new PatientNote(uow);
patient.PatientNote = patientNote;
dentist.UpdatePatientsNotes(patient, noteText, uow);
uow.CommitChanges();
}
//GS - Veryify the note was persisted
using (var uow = new UnitOfWork())
{
var criteria = new BinaryOperator("UpdateNote", noteText);
var note = uow.FindObject<Note>(criteria);
Assert.IsNotNull(note);
}
}
And that's it for this post. In this post we extended our object hierarchy by adding in our object behaviours, we also did a little refactoring to allow for new objects that came to light whilst we were adding our object behaviours and we extended our tests to ensure that our behaviours worked as advertised. Now that we have completed work on our object hierarchy, in the next post, we'll add in the XAF solution to our solution and start work on creating our web and winform applications. Hope to see you next time!