I'm trying to read about DDD, and i'm struggling a bit trying to to identify aggregate roots. I wrote a really simple app to divide players into different teams inside a game.
So my entities are something like this:
Game Entity:
public class Game : DomainEntityBase, IDomainEntity
{
private List<Team> teams = new List<Team>();
private List<Player> players = new List<Player>();
private int teamSize;
public Game(
string gameName,
int teamSize,
IEnumerable<Player> players) : base(Guid.NewGuid())
{
this.teamSize = teamSize;
this.players = players.ToList();
}
public string GameName { get; private set; }
public ReadOnlyCollection<Team> Teams => teams.AsReadOnly();
public void SplitPlayersToTeams()
{
if (players.Count() % 2 != 0)
{
throw new NotSupportedException("Only equally dividable teams are supported");
}
var teamCount = players.Count / teamSize;
var playersPerTeam = players.Count / teamCount;
SetPlayersToTeam(teamCount, playersPerTeam);
}
private void SetPlayersToTeam(int teamCount, int playersPerTeam)
{
var rnd = new Random();
for (var i = 0; i < teamCount; i++)
{
var team = new Team(i.ToString());
while (team.Players.Count != playersPerTeam)
{
var randomIndex = rnd.Next(players.Count);
var player = players[randomIndex];
if (!team.Players.Contains(player))
{
player.SetTeam(team);
team.AddPlayer(player);
}
}
teams.Add(team);
}
}
}
Team Entity:
public class Team : DomainEntityBase, IDomainEntity
{
private List<Player> players = new List<Player>();
public Team(
string teamIdentifier) : base(Guid.NewGuid())
{
TeamIdentifier = teamIdentifier;
}
public string TeamIdentifier { get; }
public ReadOnlyCollection<Player> Players => players.AsReadOnly();
public void AddPlayer(Player player)
{
players.Add(player);
}
}
Player entity:
public class Player : DomainEntityBase, IDomainEntity
{
public Player(
string nickName) : base(Guid.NewGuid())
{
Nickname = nickName;
}
public string Nickname { get; private set; }
public Team Team { get; private set; }
public void SetTeam(Team team)
{
Team = team;
}
}
Now first I was thinking that the game would be an aggregate root. It would make sense in a way. But then I started thinking that what if you want to persist players separately so that you don't have to add new players for every game? What if you want to persist teams separately if you have teams that can be re-used later? The game it self, would be aggregate root, because I would persist games to for example load a history of games from persistence.
So the question is, is every object I listed above an aggregate root, having their own repositories since every aggregate root should have it's own repository?
Thanks in advance.