简体   繁体   English

更新聚合根的 collections - 不符合 DDD。 有效的替代品?

[英]Updates on collections of Aggregate Roots - not DDD compliant. Valid alternatives?

I'm working on an Appointment Scheduling system while learning and implementing Domain Driven Design.在学习和实施领域驱动设计的同时,我正在开发一个约会计划系统。 I have identified the following Aggregate Root - ScheduleDay .我已经确定了以下聚合根 - ScheduleDay

public class ScheduleDay : Entity, IAggregateRoot
{
    // Value Object - Full datetime and year, month, week, day values as integers
    public Day Day { get; }
    // Value Object - schedule for the day
    public Schedule Schedule { get; }
    
    // Other properties 

    private readonly List<Appointment> _appointments;
    // Value Object - Appointments for the day
    public IReadOnlyCollection<Appointment> Appointments => _appointments;

    // Functions for adding/managing appointments and schedule day
}

ScheduleDay represents a single calendar day and contains the necessary information and business logic to create and manage appointments. ScheduleDay 表示单个日历日,包含创建和管理约会所需的信息和业务逻辑。

The problem - What if I want to update all Fridays with different schedule?问题- 如果我想用不同的时间表更新所有星期五怎么办?

As far as I know and have read, Domain Driven Design states that you can only work with only one aggregate at a time .据我所知并已阅读,领域驱动设计声明您一次只能使用一个聚合 This means (unless I'm wrong) you cannot load a collection of aggregates and update them at the same time.这意味着(除非我错了)您不能同时加载聚合集合并更新它们。 Even if you could - loading collection of Schedule Day Aggregates would potentially also load thousands of appointments which would greatly impact the performance, not to mention locking DB.即使你可以 - 加载计划日期聚合的集合也可能会加载数千个约会,这将极大地影响性能,更不用说锁定数据库了。

Potential solution (1)潜在解决方案 (1)

Make Schedule aggregate of its own.使 Schedule 成为它自己的聚合。 Each day of the week (for example Friday) would have its own Aggregate Root.一周中的每一天(例如星期五)都有自己的聚合根。 Then I could store Id of Schedule in ScheduleDay aggregate.然后我可以将日程表的 ID 存储在 ScheduleDay 聚合中。

public class Schedule : Entity, IAggregateRoot { }

public class ScheduleDay : Entity, IAggregateRoot
{
    public Guid Schedule { get; }
}

When I would want to update schedule for Friday, I would be updating just 1 record in DB.当我想更新星期五的时间表时,我只会更新数据库中的 1 条记录。 The problem (?) with this is, if I want to make a new Appointment, I must first load Schedule from DB, then I need to load Schedule Day - two SQL calls.这个问题(?)是,如果我想进行新的约会,我必须首先从数据库加载日程安排,然后我需要加载日程安排日 - 两个 SQL 调用。

Potential solution (2)潜在解决方案 (2)

Make Schedule Root Aggregate and ScheduleDay an Entity.使 Schedule Root Aggregate 和 ScheduleDay 成为一个实体。

public class Schedule : Entity, IAggregateRoot 
{ 
    public ScheduleDay ScheduleDay { get; }
}

public class ScheduleDay : Entity { }

With this I'm worrying about making aggregate too large (root entity with child entity that has collection of child entities), all the material I've read states that aggregates should be as small as possible.有了这个,我担心聚合太大(具有子实体集合的子实体的根实体),我读过的所有材料都表明聚合应该尽可能小。

The concept behind aggregate root is: Is ScheduleDay totally independent?聚合根背后的概念是:ScheduleDay 是完全独立的吗? can you two ScheduleDays that are equals except from the Schedule which they are related to?除了与它们相关的时间表之外,你能有两个相等的 ScheduleDays 吗?

Apart from this, the solution with Schedule as IAggregateRoot looks good.除此之外,将 Schedule 作为 IAggregateRoot 的解决方案看起来不错。 Just make sure that you are not loading all the navigations.只要确保您没有加载所有导航。 If you are using EntityFramework you can use LazyLoading proxies.如果您使用的是 EntityFramework,则可以使用 LazyLoading 代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM