在引发域事件谁负责实体的突变? DDD [英] Who is responsible for entity's mutation when domain event is raised? DDD

查看:208
本文介绍了在引发域事件谁负责实体的突变? DDD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习 CQRS / ES 。看着小例子项目中,我经常看到的事件变异实体状态的。举例来说,如果我们看看订单聚合根的:

I've been learning about CQRS/ES. Looking at small example projects I often see events mutating the entity state. For instance If we look at the Order aggregate root:

public class Order : AggregateRoot {
    private void Apply(OrderLineAddedEvent @event) {
        var existingLine = this.OrderLines.FirstOrDefault(
            i => i.ProductId == @event.ProductId);

        if(existingLine != null) {
            existingLine.AddToQuantity(@event.Quantity);
            return;
        }

        this.OrderLines.Add(new OrderLine(@event.ProductId, @event.ProductTitle, @event.PricePerUnit, @event.Quantity));
    }

    public ICollection<OrderLine> OrderLines { get; private set; }

    public void AddOrderLine(/*parameters*/) {
        this.Apply(new OrderLineAddedEvent(/*parameters*/));
    }

    public Order() {
        this.OrderLines = new List<OrderLine>();
    }

    public Order(IEnumerable<IEvent> history) {
        foreach(IEvent @event in history) {
            this.ApplyChange(@event, false);
        }
    }
}

public abstract class AggregateRoot  {
    public Queue<IEvent> UncommittedEvents { get; protected set; }

    protected abstract void Apply(IEvent @event);

    public void CommitEvents() { 
        this.UncommittedEvents.Clear();
    }

    protected void ApplyChange(IEvent @event, Boolean isNew) {
        Apply(@event);
        if(isNew) this.UncommittedEvents.Enqueue(@event);
    }
}

OrderLineAddedEvent 应用发生变异订单通过添加新订单。但我不明白这事:

when OrderLineAddedEvent is applied it mutates Order by adding new order line. But I don't understand these things:


  • 如果这是一个正确的做法怎么那么所做的更改被持久?

  • 或者我应该以某种方式从订单发布事件到相应的处理程序?我如何在技术上实现这一点?我应该使用一个服务总线来传输事件?

  • if it is a right approach how then the changes made are persisted?
  • Or should I publish the event somehow to a corresponding handler from Order? How do I implement this technically? Should I use a service bus to transmit events?

推荐答案

我也仍然ES试验所以这还是有点意见,而不是任何指导:)

I am also still experimenting with ES so this is still somewhat of an opinion rather than any guidance :)

在某个阶段,我由Jan Kronquist跨越此帖一:的 http://www.jayway.com/2013/06/20/dont-publish -domain事件回报-他们/

At some stage I came across this post by Jan Kronquist: http://www.jayway.com/2013/06/20/dont-publish-domain-events-return-them/

它的要点是,事件应该从域被返回,而不是从域内分派。这确实打动了我。

The gist of it is that event should be returned from the domain rather than being dispatched from within the domain. This really struck a chord with me.

如果一个人要采取更加的传统的的方法,即使用普通的面向持久化存储库中的应用层的会处理事务和库的访问。该域名将被简单地称为执行的行为。

If one were to take a more traditional approach where a normal persistence-oriented repository is used the Application Layer would handle transactions and repository access. The domain would simply be called to perform the behaviour.

此外,域名应该始终坚持持久性的无知。其总根保持事件的列表似乎总是有点怪我,我绝对不喜欢我的人工鱼礁一些公共基础继承。它不觉得不够干净

Also, the domain should always stick to persistence ignorance. Having an aggregate root maintain a list of events always seemed somewhat odd to me and I definitely do not like having my ARs inheriting from some common base. It does not feel clean enough.

所以把这个一起使用是否有什么:

So putting this together using what you have:

public OrderLineAddedEvent AddOrderLine(/*parameters*/) {
    return this.Apply(new OrderLineAddedEvent(/*parameters*/));
}

在我的POC我也没有被使用 IEvent 标记接口,而只是一个对象

In my POC I have also not been using an IEvent marker interface but rather just an object.

现在的应用程序层的是早在持久性的控制。

Now the Application Layer is back in control of the persistence.

我有一个实验性的GitHub库去:

I have an experimental GitHub repository going:

  • https://github.com/Shuttle/shuttle-recall-core
  • https://github.com/Shuttle/shuttle-recall-sqlserver

我还没来得及看它一会儿,我知道我已经做了一些改动,但我们欢迎您来看看。

I haven't had time to look at it for a while and I know I have already made some changes but you are welcome to have a look.

其基本思想是那么认为的应用层的会使用 EventStore / EventStream 来管理以同样的方式以总事件的的应用层的会使用。在 EventStream 将被应用到的聚集体。所有事件从域的行为将被添加到 EventStream 后,它再次坚持返回。

The basic idea is then that the Application Layer will use the EventStore/EventStream to manage the events for an aggregate in the same way that the Application Layer would use a Repository. The EventStream will be applied to the aggregate. All events returned from the domain behaviours would be added to the EventStream after which it is persisted again.

这不断所有面向持久位出域。

This keeps all the persistence-oriented bits out of the domain.

这篇关于在引发域事件谁负责实体的突变? DDD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆