There are two entities around this problem, mainly
- Leaderboard - Which holds the info about type(lowest first/highest first), description, name etc.
- Score - The score value submitted by the player which holds player details along with score value
Usecases:
- We need to fetch scored who are top 10
- For a monthly leaderboard, we need to find top 3
Domain Rules:
- A player can submit any number of scores
- The leaderboard ranking needs to be based on the type defined in leaderboard (lowest/highest)
For such a system where
- Leaderboard and scores has 1 to many relationship
- Score needs to have info about the player information(which is a separate aggregate root and in different Bounding Context)
How to design it in DDD?
Scenario 1: Does Leaderboard will be aggregate root and Scores will be added through Leaderboard aggregate root (for every score)?
Queries:
Here, scores doesn't have a meaning without Leaderboard, and also no domain rules insist to add a score via Leaderboard aggregate root. This is in-fact a dilemma and how to handle this?
How to get the Player details to feed in score? Do I need to fetch the player details in a domain service and feed the Leaderboard Aggregate root while adding the score?
Scenario 2: Leaderboard and LeaderboardScore are two different Aggregate roots.
Queries:
While calculating ranks, we need to fetch scores from score aggregate root and type info from leaderboard and fulfil the use-case?
Here most of the use-case serving code needs to be in Domain Service or Application Service?
I would approach it with score and leaderboard being their own aggregates. Score changes publish domain events which get fed (asynchronously, since eventual consistency is probably OK) to update the leaderboard.