Designing Leaderboard Scores in DDD

136 Views Asked by At

There are two entities around this problem, mainly

  1. Leaderboard - Which holds the info about type(lowest first/highest first), description, name etc.
  2. Score - The score value submitted by the player which holds player details along with score value

Usecases:

  1. We need to fetch scored who are top 10
  2. For a monthly leaderboard, we need to find top 3

Domain Rules:

  1. A player can submit any number of scores
  2. The leaderboard ranking needs to be based on the type defined in leaderboard (lowest/highest)

For such a system where

  1. Leaderboard and scores has 1 to many relationship
  2. 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:

  1. 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?

  2. 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:

  1. While calculating ranks, we need to fetch scores from score aggregate root and type info from leaderboard and fulfil the use-case?

  2. Here most of the use-case serving code needs to be in Domain Service or Application Service?

1

There are 1 best solutions below

3
On

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.