您是否在 CQRS 中使用聚合根 (AR) 类型作为事件参数?
您是否使用聚合根( AR)事件参数的类型?
例如,第一个方法适用于简单类型,但第二个方法使用复杂类型。为什么你更喜欢第一个或第二个?
AggregateRoot()
{
private SomeAR _someAR;
public void DoSomething(SomeAR someAR)
{
// Validation and other similar stuff.
ApplyEvent(new SomethingHappenedEvent(someAR.StringText));
}
public void DoAnything(SomeAR someAR )
{
ApplyEvent(new NothingHappenedEvent(this, someAR));
}
protected void OnDoSomething(SomethingHappenedEvent e)
{
_someAR = ???
}
}
Do you use aggregate root (AR) types for event parameters?
For example, the first method works with simple types but the second uses complex types. Why do you prefer the first or the second?
AggregateRoot()
{
private SomeAR _someAR;
public void DoSomething(SomeAR someAR)
{
// Validation and other similar stuff.
ApplyEvent(new SomethingHappenedEvent(someAR.StringText));
}
public void DoAnything(SomeAR someAR )
{
ApplyEvent(new NothingHappenedEvent(this, someAR));
}
protected void OnDoSomething(SomethingHappenedEvent e)
{
_someAR = ???
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请质疑为什么要传递该实体和/或聚合。从这些物体中你真正想要的是什么?我告诉你,是它们的状态或无副作用函数产生了你想要放入该事件的状态。
将事件耦合到聚合/实体对于消费者来说会很困难,尤其是在涉及消息传递的情况下。很快您就会在聚合/实体的状态(无论是字段还是属性)上添加序列化属性。也许您将开始引入状态,以便能够在序列化时为事件提供所需的数据。坏主意,因为你给你的聚合/实体一个责任 - 出于其自身行为以外的目的跟踪状态 - 他们一开始就不应该这样做。
这不仅是耦合和消息传递,也是一个选择问题:在内部(聚合/实体)和外部(事件/命令)以不同方式表示事物的自由。大多数内部领域对象都与行为有关,而在外部,事件/命令对象则与携带状态和意图有关。
综上所述,双重调度到一个对象以达到其状态并没有什么问题。
Do question why you are passing in that entity and/or aggregate. What is it that you are really after from those objects? I'll tell you, it's their state or a side-effect free function that produces state you want to put in that event.
Coupling your events to your aggregates/entities is going to be hard on your consumers, especially if messaging is involved. Pretty soon you'll be sprinkling serialization attributes on your aggregate's/entity's state (be it fields or properties). Maybe you'll start to introduce state to be able to give the event the data it wants upon serialization. Bad idea, because you're giving your aggregates/entities a responsibility - keeping track of state for purposes other than its own behavior - they shouldn't have in the first place.
It's not only the coupling and messaging, it's also a matter of choice: the freedom to represents things differently on the inside (aggregates/entities) and on the outside (events/commands). Most domain objects on the inside are all about behavior, while on the outside the event/command objects are about carrying state and intent forward.
All that said, there's nothing wrong with double-dispatching into an object to get to it's state.
我会保持简单,只在事件中传递最少量的数据。足以让事件处理程序对聚合根进行适当的状态更改。这在序列化事件以进行消息传递和存储到事件存储时会有所帮助。
I'd keep it simple and only pass the minimal amount of data within the event. Just enough for the event handler to make appropriate state changes to your aggregate root. This will help when serializing your events for messaging and storing to an event store.
据我的理解,事件只能有基本数据类型或最多值对象。我们不能有聚合根 (AR) 事件中。第二个建议是不要在另一个 AR 中使用一个 AR。如果说我有 AR A 和实体 B,我会这样写:
As far as my understanding goes, events can only have basic data types or at most value objects. We cannot have an aggregate root (AR) in the event. The second recommendation would be not to use an AR in another AR. If say I have AR A and Entity B, I will write it like this: