加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

事件源聚合根是否应该访问事件源库?

发布时间:2020-12-13 20:45:45 所属栏目:百科 来源:网络整理
导读:我正在使用应用程序/域层中的DDD进行 event-sourced CQRS实现.我有一个如下所示的对象模型: public class Person : AggregateRootBase{ private Guid? _bookingId; public Person(Identification identification) { Apply(new PersonCreatedEvent(identific
我正在使用应用程序/域层中的DDD进行 event-sourced CQRS实现.我有一个如下所示的对象模型:
public class Person : AggregateRootBase
{
    private Guid? _bookingId;

    public Person(Identification identification)
    {
        Apply(new PersonCreatedEvent(identification));
    }

    public Booking CreateBooking() {
        // Enforce Person invariants
        var booking = new Booking();
        Apply(new PersonBookedEvent(booking.Id));
        return booking;
    }

    public void Release() {
        // Enforce Person invariants
        // Should we load the booking here from the aggregate repository?
        // We need to ensure that booking is released as well.
        var booking = BookingRepository.Load(_bookingId);
        booking.Release();
        Apply(new PersonReleasedEvent(_bookingId));
    }

    [EventHandler]
    public void Handle(PersonBookedEvent @event) { _bookingId = @event.BookingId; }

    [EventHandler]
    public void Handle(PersonReleasedEvent @event) { _bookingId = null; }
}

public class Booking : AggregateRootBase
{
    private DateTime _bookingDate;
    private DateTime? _releaseDate;

    public Booking()
    {
        //Enforce invariants
        Apply(new BookingCreatedEvent());
    }

    public void Release() 
    {
        //Enforce invariants
        Apply(new BookingReleasedEvent());
    }

    [EventHandler]
    public void Handle(BookingCreatedEvent @event) { _bookingDate = SystemTime.Now(); }
    [EventHandler]
    public void Handle(BookingReleasedEvent @event) { _releaseDate = SystemTime.Now(); }
    // Some other business activities unrelated to a person
}

到目前为止,由于我对DDD的理解,Person和Booking都是单独的聚合根源,原因有两个:

>有时,业务组件会将Booking对象与数据库分开. (即,由于信息不正确,已被释放的人之前的预订已被修改).
>每当需要更新预订时,不应该在人员和预订之间锁定争用.

另一个业务要求是,对于一个人来说,一次不能进行预订.因此,我担心在读取端查询查询数据库,因为那里可能存在一些不一致(由于使用CQRS并且具有最终一致的读取数据库).

是否允许聚合根通过id为对象查询事件源后备存储(根据需要延迟加载它们)?是否有任何其他实施途径更有意义?

首先,你真的需要事件采购吗?这看起来很简单.事件采购既有优点也有缺点.虽然它为您提供了免费的审计跟踪并使您的域模型更具表现力,但它使解决方案变得复杂.

好吧,我认为在这一点上你考虑了你的决定,你决心继续参与活动.我认为你错过了消息传递作为聚合之间通信手段的概念.它在Pat Helland’s paper中被描述得最好(也就是BTW,不是关于DDD或事件源,而是关于可伸缩性).

这个想法是聚合可以相互发送消息来强制某些行为.聚合之间不能进行同步(a.k.a方法调用)交互,因为这会引入一致性问题.

在您的示例中,Person AR会向预订AR发送预约消息.此消息将以某种异步且可靠的方式传输.预订AR将处理此消息,如果已经由另一个人预订,它将回复ReservationRejected消息.否则,它会发送ReservationConfirmed.这些消息必须由Person AR处理.也许,他们会产生另一个事件,这个事件将被转换成发送给客户的电子邮件或类似的东西.

无需在模型中获取查询数据.只是消息.如果您想要一个示例,可以下载Ncqrs项目的“Messaging”分支的源代码并查看ScenarioTest类.它使用来自Blue Book的Cargo和HandlingEvent示例演示了AR之间的消息传递.

这回答了你的问题了吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读