非聚合根可以保存另一个非聚合根的引用吗?

问题描述 投票:5回答:1

如果我有这样的two aggregates

第一集合:

  • WorktimeRegulation(Root)
  • 工作时间
  • RegulationEnrolment

澄清数据:

工作时间规定:

 public class WorkTimeRegulation : Entity<Guid>, IAggregateRoot
    {
        private WorkTimeRegulation()//COMB
       : base(Provider.Sql.Create()) // required for EF
        {
        }
        private WorkTimeRegulation(Guid id) : base(id)
        {
            _assignedWorkingTimes = new List<WorkingTime>();
            _enrolledParties = new List<RegulationEnrolment>();
        }
        private readonly List<WorkingTime> _assignedWorkingTimes;
        private readonly List<RegulationEnrolment> _enrolledParties;
        public string Name { get; private set; }
        public byte NumberOfAvailableRotations { get; private set; }
        public bool IsActive { get; private set; }
        public virtual IEnumerable<WorkingTime> AssignedWorkingTimes { get => _assignedWorkingTimes; }
       public virtual IEnumerable<RegulationEnrolment> EnrolledParties { get => _enrolledParties; }
        //...
    }

Id|    Name            |   NumberOfAvailableRotations|  IsActive 

 1|    General Rule    |          2                  |    true   

工作时间 :

public class WorkTime : Entity<Guid>
    {
        private WorkTime()
      : base(Provider.Sql.Create()) // required for EF
        {
        }
        private WorkTime(Guid id) : base(id)
        {
            ActivatedWorkingTimes = new List<WorkingTimeActivation>();
        }
        private ICollection<WorkingTimeActivation> _activatedWorkingTimes;

        public string Name { get; set; }
        public byte NumberOfHours { get; set; }
        public byte NumberOfShortDays { get; set; }
        public Guid WorkTimeRegulationId { get; private set; }
        public virtual ICollection<WorkingTimeActivation> ActivatedWorkingTimes { get => _activatedWorkingTimes; private set => _activatedWorkingTimes = value; }
        //....
   }

Id|  Name   |   NumberOfHours| NumberOfShortDays |WorkTimeRegulationId 

1 | Winter  |     8          |    1              |    1
2 | Summer  |     6          |    0              |    1

第二集合:

  • 转移(根)
  • ShiftDetail
  • ShiftEnrolment

澄清数据:

转变:

  public class Shift : Entity<Guid>, IAggregateRoot
    {
        private readonly List<ShiftDetail> _assignedShiftDetails;
        private readonly List<ShiftEnrolment> _enrolledParties;


        public string Name { get; set; }
        public ShiftType ShiftType { get; set; }
        public int WorkTimeRegulationId { get; set; }
        public bool IsDefault { get; set; }
        public virtual WorkingTimeRegulation WorkTimeRegulation { get; set; }
        public virtual IEnumerable<ShiftDetail> AssignedShiftDetails { get => _assignedShiftDetails; }
        public virtual IEnumerable<ShiftEnrolment> EnrolledParties { get => _enrolledParties; }
        //...........
   }

Id|  Name      |  ShiftType  |  WorkTimeRegulationId  | IsDefault 
1 | IT shift   |  Morning    |    1                   |  1 

ShiftDetail:

  public class ShiftDetail : Entity<Guid>
    {
        public Guid ShiftId { get; private set; }
        public Guid WorkTimeId { get; private set; }
        public DateTimeRange ShiftTimeRange { get; private set; }
        public TimeSpan GracePeriodStart { get; private set; }
        public TimeSpan GracePeriodEnd { get; private set; }
        public virtual WorkTime WorkTime { get; private set; }

        private ShiftDetail()
        : base(Provider.Sql.Create()) // required for EF
        {
        }
        //..........
   }

ShiftId  WorkTimeId shift-start  shift-end   
  1          1        08:00        16:00
  1          2        08:00        14:00

我的问题在这里:

  • 非聚合根(ShiftDetail)是否可以保存另一个非聚合根(WorkTime)的引用?
  • 领域专家澄清:要创建一个有效的转变,那么我们应该为与特定shift detail相关的每个worktime都有一个worktimeRegulation。如果在worktime中有参考,则无法更新shiftDetails的工作时数。前面的例子显示我们有two worktimes(winter,summer),所以我们有一个shiftdetail为winter坚持8工作时间和shiftdetailsummer坚持6working小时。现在我觉得由非聚合根控制的移位细节不变(worktime)如何强制这个不变量?
  • 根据以前的信息,我是否犯了与聚合规格有关的错误?
c# oop domain-driven-design aggregation aggregateroot
1个回答
1
投票

非聚合根(ShiftDetail)是否可以保存另一个非聚合根(WorkTime)的引用?

不,除非它们存在于同一聚合中。

您可以仅保留对其他Aggregate root的ID的引用。

您可以从另一个Aggregate中获取对嵌套实体的ID的引用,但是您应该注意到此ID是不透明的,您可能不会假设它是如何在内部使用它的聚合根来查找嵌套实体。

现在我觉得由非聚合根(工作时间)控制的移位细节的不变量如何强制这个不变量?

您可以通过两种方式强制执行不变量:

  1. 在聚合内部。这意味着Aggregate必须足够大,它必须拥有它需要的所有状态。这种执法非常一致。
  2. 由Saga /流程经理协调。此组件对可能的多个Aggregates内的更改做出反应,并将命令发送到其他Aggregates。 Saga与Aggregate相反。这种执法最终是一致的。
© www.soinside.com 2019 - 2024. All rights reserved.