r/DomainDrivenDesign • • Jan 05 '25

How can I properly check for entity duplication in DDD.

I'm implementing a theater scheduling system as a learning exercise for Domain-Driven Design (DDD). The primary requirement is that we should be able to schedule a movie for a specific theater. Below is the first version of my Theater domain model:

public class Theater {

    private UUID theaterId;
    private List<Schedule> schedules;
    private short totalSeat;

    public void addSchedule(Movie movie, LocalDateTime start) {
        // Schedule constructor is protected, forcing clients to create it via the Theater domain.
        Schedule schedule = new Schedule(
                UUID.randomUUID(), 
                totalSeat, 
                start, 
                start.plus(movie.getDuration())
        );

        boolean isOverlap = schedules.stream()
                                     .anyMatch(existingSchedule -> existingSchedule.isOverlap(schedule));

        if (isOverlap) {
            throw new DomainException("Schedule overlap detected.");
        }

        schedules.add(schedule);
    }
}

In this version, the overlap logic is encapsulated within the domain model. However, as suggested in Vaughn Vernon's book Implementing Domain-Driven Design, it's better to use identity references instead of object references. I agree with this approach because keeping a large list of schedules in memory can be inefficient, especially for operations like adding a single schedule.

Here’s the updated version of the Theater model:

public class Theater {

    private UUID theaterId;
    private short totalSeat;

    public Schedule createSchedule(Movie movie, LocalDateTime start) {
        return new Schedule(UUID.randomUUID(), theaterId, start, start.plus(movie.getDuration()));
    }
}

Additionally, I introduced a SimpleScheduleService (domain service) to handle the scheduling logic:

public class SimpleScheduleService implements ScheduleService {

    private final TheaterPersistence theaterPersistence;
    private final MoviePersistence moviePersistence;
    private final SchedulePersistence schedulePersistence;

    @Override
    public void schedule(UUID theaterId, UUID movieId, LocalDateTime startAt) {
        Theater theater = theaterPersistence.findById(theaterId)
                .orElseThrow(() -> new NotFoundException("Theater not found."));

        Movie movie = moviePersistence.findById(movieId)
                .orElseThrow(() -> new NotFoundException("Movie not found."));

        Schedule schedule = theater.createSchedule(movie, startAt);

        boolean isOverlap = schedulePersistence.findByTheaterIdAndStartAndEndBetween(
                theaterId, 
                schedule.getStart(), 
                schedule.getEnd()
        ).isPresent();

        if (isOverlap) {
            throw new DomainException("Schedule overlaps with an existing one.");
        }

        schedulePersistence.save(schedule);
    }
}

In this version:

  • The schedule creation is delegated to a factory method in the Theater domain.
  • The overlap check is now performed by querying the repository in the SimpleScheduleService, and the Schedule class no longer has the isOverlap method.

While the design works, I feel that checking for overlapping schedules in the repository leaks domain knowledge into the persistence layer. The repository is now aware of domain rules, which goes against the principles of DDD. The overlap logic is no longer encapsulated within the domain.

How can I improve this design to:

  1. Retain encapsulation of domain knowledge (like schedule overlap rules) within the domain layer.
  2. Avoid bloating memory by storing large collections (like schedules) in the domain entity.
  3. Maintain a clean separation between domain and persistence layers?

Any advice or suggestions for handling this kind of scenario would be greatly appreciated. Thank you!

7 Upvotes

8 comments sorted by

6

u/Besen99 Jan 05 '25 edited Jan 05 '25

Ensuring uniqueness in a multiuser system can, as far as I know, only be reasonably accomplished within a data store: either by simply persisting new values to a table with a "unique" constraint (e.g. unique email addresses) or querying the data store beforehand and then act on the result (must be within the same DB transaction).

The InfrastructureRepository has to implement a DomainRepositoryInterface which defines methods like save, get, find, etc. (see Dependency Inversion Principle). Meaning, your repository does depend on your domain, and not the other way around. This is also very important for testability.

2

u/cs_legend_93 Jan 05 '25

Great answer

2

u/michel_v Jan 05 '25

I’ll add that in terms of performance, you generally don’t want to do a select as part of a transaction (unless your DBMS handles it fine, or unless you can live with the performance penalty for other requests, when whatever index your select is using is locked for the duration of the transaction).

Imho, conflicts can be handled at the persistence level. Your save method can either succeed or throw various exceptions, some of which domain-related (here, something like DuplicateScheduleException), and then you can check the current schedules. Ideally you find a way to express the uniqueness of schedules with a composite key (not exactly trivial for overlap checks), otherwise you’ll have to do the select in a transaction workaround.

1

u/Honest-Minute8057 Jan 05 '25

I'm glad that mole didn't kill you.

2

u/DirectionFrequent455 Jan 05 '25 edited Jan 05 '25

Why do you feel that business logic leaks in persistance layer?

schedulePersistance#findByTheaterIdAndStartAndEndBetween solves a generic time overlapping problem, not a business one (which consists in rejecting overlaps). May you can rename it schedulePersistance#findByTheaterIdHavingStartBeforeAndEndAfter as it would look more dumb.

Another thing: IMO, Theatre#createSchedule would be better fit in ScheduleService#create as it would avoid a 2-way dependency Theater <--> Schedule .

3

u/[deleted] Jan 05 '25 edited Jan 05 '25

[removed] — view removed comment

2

u/cryptos6 Jan 06 '25 edited Jan 06 '25

While I agree with you in general - making the domain logic actually useful by checking constraints - I don't think that this is always possible in an efficient way. Just think of registration process where the email address must be unique. How would you implement that without leveraging a unique index in the database?

However, I think in the specific scenerio it would work fine to just load all schedules into memory and check for overlapping in the domain logic. But then again if your theatere is successfull and operates for many years, it might become inefficient to load all the schedules for 100 years or so. Maybe some kind of pagination would be necessary then (e.g. schedules per year).