提问者:小点点

DDD一对多,具有多个属性到父亲的单个id


这是有DDD经验的人的问题。如果我们有一个子实体,它有两个属性,但都是父项的FK,那么设计子实体的最佳方法是什么?例如,我们有用户实体,而你有公司实体,它有两个属性,声明ManagerId和DirectorId都应该是用户实体的ID的FK。

这个代码是好的实践吗?

public class User: IAggregateRoot 
{
      public int Id {get; private set;}
      public string Name {get;private set;}
      private readonly List<Company> _directorCompanies = new List<Company>();
      public List<Company> DirectorCompanies => _directorCompanies.AsReadOnly();
      private readonly List<Company> _managerCompanies = new List<Company>();
      public List<Company> ManagerCompanies => _managerCompanies.AsReadOnly();
      public User(string name)
      {
        Name = name
      }
} 
public class Company: IAggregateRoot
{
  public int Id {get;private set;}
  public string Name {get;private set;}
  public int DirectorId {get;private set;}
  public int? ManagerId {get;private set;}
  public User Director {get;private set;}
  public User Manager {get;private set;}
  public Company(string name, int directorId, int? manangerid)
  {
    Name = name;
    DirectorId = directorId;
    ManagerId = managerId;
  }
}

有了这个解决方案,以后我可以为特定的公司命名,如果有的话,谁是董事,谁是经理。否则,如果我这样管理:

 public class User: IAggregateRoot 
    {
          public int Id {get; private set;}
          public string Name {get;private set;}
          public User(string name)
          {
            Name = name
          }
    } 
    public class Company: IAggregateRoot
    {
      public int Id {get;private set;}
      public string Name {get;private set;}
      public int DirectorId {get;private set;}
      public int? ManagerId {get;private set;}

      public Company(string name, int directorId, int? manangerid)
      {
        Name = name;
        DirectorId = directorId;
        ManagerId = managerId;
      }
    }

如何在存储库中使用include获取名称?我关心的是什么是正确的方式,好的方式,来实施这种情况,在需要的时候我可以获得董事或经理的信息。当做


共1个答案

匿名用户

我认为你的问题在于理解OO编程的基础知识,

public class User: IAggregateRoot 
{
      public int Id {get; private set;}
      public string Name {get;private set;}
      private readonly List<Company> _directorCompanies = new List<Company>();
      public List<Company> DirectorCompanies => _directorCompanies.AsReadOnly();
      private readonly List<Company> _managerCompanies = new List<Company>();
      public List<Company> ManagerCompanies => _managerCompanies.AsReadOnly();
      public User(string name)
      {
        Name = name
      }
}

这种_directorCompanies_managerCompanies没有多大意义,因为用户可以是经理,经理必须是用户,但不是所有的用户都是经理或董事。我认为这个设计的方式是:

public class User: IAggregateRoot 
{
      public int Id {get; private set;}
      public string Name {get;private set;}
} 

public class Company: IAggregateRoot
{
  public int Id {get;private set;}
  public string Name {get;private set;}
  public int DirectorId {get;private set;}
  public int? ManagerId {get;private set;}
  public virtual User Director {get;private set;}
  public virtual User Manager {get;private set;}  
}

是你展示的东西,但是遵循OO的思维方式。

对于查询,示例:

作为管理者的用户:

myContext.Users.Where(c => myContext.Managers.Contains(m => c.Id));