基于消息的域对象协作

发布于 2024-10-16 22:01:37 字数 1413 浏览 9 评论 0原文

最近在思考如何利用消息来实现领域对象协作。现在我有一些想法:

  1. 如果每个域对象想要响应一条消息,它将实现一个接口;
  2. 每个域对象不会依赖于任何其他域对象,这意味着我们不会有 Model.OtherModel 关系;
  3. 每个域对象只做只修改自身的事情;
  4. 每个域对象可以发送一条消息,并且该消息将被任何其他关心该消息的域对象接收;

总的来说,领域对象之间协作的唯一方式就是消息,一个领域对象可以根据需要发送任何消息或接收任何消息。

当我学习Evans的DDD时,我看到他在领域中定义了聚合概念,我认为聚合是静态的,不适合对象交互,他只关注对象的静态结构或对象之间的关系。在现实世界中,对象将使用消息进行交互,而不是通过相互引用或聚合其他对象。在我看来,所有对象都是平等的,这意味着它们不会依赖于任何其他对象。 对于如何实现发送消息或接收消息的功能,我认为我们可以创建一个专门用于领域对象协作的EventBus框架。我们可以将事件类型映射到字典中的订阅者类型。键是事件类型,值是订阅者类型列表。当一个事件发生时,我们可以找到相应的订阅者类型,并从数据持久化中获取所有订阅者域对象,然后在每个订阅者上调用相应的句柄方法。

例如:

public class EventA : IEvent { }
public class EventB : IEvent { }
public class EventC : IEvent { }

public class ExampleDomainObject : Entity<Guid>{
    public void MethodToRaiseAnExampleEvent()
    {
        RaiseEvent(new EventC());
    }
}
public class A : Entity<Guid>, IEventHandler<EventB>, IEventHandler<EventC> {
    public void Handle(EventB evnt)
    {
        //Response for EventB.
    }
    public void Handle(EventC evnt)
    {
        //Response for EventC.
    }
}
public class B : IEventHandler<EventA>, IEventHandler<EventC> {
    public void Handle(EventA evnt)
    {
        //Response for EventA.
    }
    public void Handle(EventC evnt)
    {
        //Response for EventC.
    }
}

这就是我的想法。希望能听到您的话。

Recently, i was thinking about how to use message to implement the domain object collaboration. And now i have some thoughts:

  1. Each domain object will implement an interface if it want to response one message;
  2. Each domain object will not depend on any other domain objects, that means we will not have the Model.OtherModel relation;
  3. Each domain object only do the things which only modify itself;
  4. Each domain object can send a message, and this message will be received by any other domain objects which are care about this message;

Totally, the only way of collaboration between domain objects is message, one domain object can send any messages or receive any messages as long as it need.

When i learn Evans's DDD, i see that he defines the aggregate concept in domain, i think aggregate is static and not suitable for objects interactions, he only focused on the static structure of objects or relationship between objects. In real world, object will interact using messages, not by referencing each other or aggregating other objects. In my opinion, all the objects are equal, that means they will not depend on any other objects.
For about how to implement the functionality of sending messages or receive messages, i think we can create a EventBus framework which is specially used for the collaboration of domain object. We can mapping the event type to the subscriber type in a dictionary. The key is event type, the value is a list of subscriber types. When one event is raised, we can find the corresponding subscriber types, and get all the subscriber domain objects from data persistence and then call the corresponding handle methods on each subscriber.

For example:

public class EventA : IEvent { }
public class EventB : IEvent { }
public class EventC : IEvent { }

public class ExampleDomainObject : Entity<Guid>{
    public void MethodToRaiseAnExampleEvent()
    {
        RaiseEvent(new EventC());
    }
}
public class A : Entity<Guid>, IEventHandler<EventB>, IEventHandler<EventC> {
    public void Handle(EventB evnt)
    {
        //Response for EventB.
    }
    public void Handle(EventC evnt)
    {
        //Response for EventC.
    }
}
public class B : IEventHandler<EventA>, IEventHandler<EventC> {
    public void Handle(EventA evnt)
    {
        //Response for EventA.
    }
    public void Handle(EventC evnt)
    {
        //Response for EventC.
    }
}

That's my thoughts. Hopes to hear your words.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可遇━不可求 2024-10-23 22:01:37

您听说过事件溯源 还是 CQRS

听起来这就是你的想法的方向。

有很多很棒信息 在那里。许多关于 CQRS域事件基于消息传递的域

一些 示例 实现可用,并且有一个有用且活跃的社区,可以在其中讨论实现细节。

Have you ever heard of event sourcing or CQRS?

It sounds like that's the direction your thoughts are heading.

There's a lot of great info out there. Many good blog posts about CQRS and Domain Events, messaging-based domains.

Some example implementations are available, and there's a helpful and active community where implementation details can be discussed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文