OverflowStack
OverflowStack

Reputation: 919

Updates on collections of Aggregate Roots - not DDD compliant. Valid alternatives?

I'm working on an Appointment Scheduling system while learning and implementing Domain Driven Design. I have identified the following Aggregate Root - ScheduleDay.

public class ScheduleDay : Entity, IAggregateRoot
{
    // Value Object - Full datetime and year, month, week, day values as integers
    public Day Day { get; }
    // Value Object - schedule for the day
    public Schedule Schedule { get; }
    
    // Other properties 

    private readonly List<Appointment> _appointments;
    // Value Object - Appointments for the day
    public IReadOnlyCollection<Appointment> Appointments => _appointments;

    // Functions for adding/managing appointments and schedule day
}

ScheduleDay represents a single calendar day and contains the necessary information and business logic to create and manage appointments.

The problem - What if I want to update all Fridays with different schedule?

As far as I know and have read, Domain Driven Design states that you can only work with only one aggregate at a time. This means (unless I'm wrong) you cannot load a collection of aggregates and update them at the same time. Even if you could - loading collection of Schedule Day Aggregates would potentially also load thousands of appointments which would greatly impact the performance, not to mention locking DB.

Potential solution (1)

Make Schedule aggregate of its own. Each day of the week (for example Friday) would have its own Aggregate Root. Then I could store Id of Schedule in ScheduleDay aggregate.

public class Schedule : Entity, IAggregateRoot { }

public class ScheduleDay : Entity, IAggregateRoot
{
    public Guid Schedule { get; }
}

When I would want to update schedule for Friday, I would be updating just 1 record in DB. The problem (?) with this is, if I want to make a new Appointment, I must first load Schedule from DB, then I need to load Schedule Day - two SQL calls.

Potential solution (2)

Make Schedule Root Aggregate and ScheduleDay an Entity.

public class Schedule : Entity, IAggregateRoot 
{ 
    public ScheduleDay ScheduleDay { get; }
}

public class ScheduleDay : Entity { }

With this I'm worrying about making aggregate too large (root entity with child entity that has collection of child entities), all the material I've read states that aggregates should be as small as possible.

Upvotes: 0

Views: 254

Answers (1)

ddfra
ddfra

Reputation: 2565

The concept behind aggregate root is: Is ScheduleDay totally independent? can you two ScheduleDays that are equals except from the Schedule which they are related to?

Apart from this, the solution with Schedule as IAggregateRoot looks good. Just make sure that you are not loading all the navigations. If you are using EntityFramework you can use LazyLoading proxies.

Upvotes: 1

Related Questions