Aggregate Object

In Domain-Driven Design (DDD), an aggregate is a cluster of domain objects that can be treated as a single unit. It is often composed of an entity that serves as the root and a set of related entities and value objects. Aggregates are used to enforce consistency and transactional boundaries within a domain.
   
  
public class Patient : Entity
{
    public string Name { get; private set; }
    public List Appointments { get; private set; }

    public Patient(string name)
    {
        Name = name;
        Appointments = new List();
    }

    public void ScheduleAppointment(DateTime date, Doctor doctor)
    {
        // Additional logic to check scheduling constraints
        Appointments.Add(new Appointment(date, doctor));
    }

    // Other methods and properties...
}

public class Appointment : ValueObject
{
    public DateTime Date { get; private set; }
    public Doctor Doctor { get; private set; }

    public Appointment(DateTime date, Doctor doctor)
    {
        Date = date;
        Doctor = doctor;
    }

    // Equality and hash code methods...
}

public class Doctor : Entity
{
    public string Name { get; private set; }
    public string Specialty { get; private set; }

    public Doctor(string name, string specialty)
    {
        Name = name;
        Specialty = specialty;
    }

    // Other methods and properties...
}


In this example, Patient is an entity and serves as the root of the aggregate. It contains a list of Appointments, each represented by the Appointment value object. The Doctor entity represents a doctor who can be scheduled for appointments. Together, these classes form an aggregate that manages the scheduling of patient appointments.

Comments