1

I am creating an app to create matches and have stucked with a problem of many-to-many definition in DDD approach.

I have an entity Player which describes player in my domain and have collection of Matches there.

 public class Player: MyEntity
    {
   
        public string UserName { get; private set; }

        public virtual Guid UserId { get; private set; }

        public ICollection<Match> Matches { get; private set; }

        private Player() 
        { }

        ...

        ...
    }

UPDATED ENTITY CLASS DEFINITION

As well I have domain entity Match where I have 2 references for Teams, i.e TeamA and TeamB

public class Match: MyEntity
    {
        public MatchStatus Status { get; private set;  }

        public GameType GameType { get; private set; }

        public Team TeamA { get; private set; }

        public Team TeamB { get; private set; }

        public Match(MatchStatus status, GameType gameType)
        {
            UpdateStatus(status);
            GameType = gameType;
        }

        public Match()
        {
 
        }

        ...
    }
public class Team: MyEntity
    {
        public string Side { get; private set; }

        public virtual IReadOnlyCollection<Player> Players => _players.AsReadOnly();
        private List<Player> _players = new List<Player>();

        private Team()
        { }

        public Team(string side)
        {
            Side = side;
        }

        public void AddPlayerToTeam(Player player)
        {
            if (_players.Any(x=>x.UserId == player.UserId)) throw new ArgumentException("Player is already in team");

            _players.Add(player);
        }
  
    }

My questions: is it possible approach? How should I then configrue EF Core to match this relation without creating another anemic class PlayerMatch which I suppose will break DDD?

Another question, how it will be stored in database after proper configured in EF Core? Will it create another table PlayerMatch like on picture ?

enter image description here

Thank for any help!

hoozr
  • 403
  • 4
  • 15

0 Answers0