我在 Masstransit 中定义了以下状态机:
public class OrderStateMachine : MassTransitStateMachine<OrderState>
{
public OrderStateMachine()
{
InstanceState(x => x.Status);
Event(() => OrderCreated, x => x.CorrelateBy(order => order.OrderCode, ctx => ctx.Message.OrderCode).SelectId(ctx => NewId.NextGuid()));
//How should I select an id for these events?
Event(() => OrderProvisioned, x => x.CorrelateBy(order => order.OrderCode, ctx => ctx.Message.OrderCode));
Event(() => OrderInvoiced, x => x.CorrelateBy(order => order.OrderCode, ctx => ctx.Message.OrderCode));
State(() => Created);
State(() => Finished);
CompositeEvent(() => OrderFinished, order => order.CompositeStatus, OrderProvisioned, OrderInvoiced);
Initially(
When(OrderCreated)
.Then(context => Console.WriteLine("Order created"))
.TransitionTo(Created));
During(Created,
When(OrderFinished)
.Then(context => Console.WriteLine("Order finished"))
.TransitionTo(Finished)
.Finalize());
}
public State Created { get; set; }
public State Finished { get; set; }
public Event<OrderCreated> OrderCreated { get; set; }
public Event<OrderProvisioned> OrderProvisioned { get; set; }
public Event<OrderInvoiced> OrderInvoiced { get; set; }
public Event OrderFinished { get; set; }
}
public class OrderState : SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public string OrderCode { get; set; }
public string Status { get; set; }
public CompositeEventStatus CompositeStatus { get; set; }
}
public class OrderCreated
{
public string OrderCode { get; set; }
public OrderCreated(string orderCode)
{
OrderCode = orderCode;
}
}
public class OrderInvoiced
{
public string OrderCode { get; set; }
public OrderInvoiced(string orderCode)
{
OrderCode = orderCode;
}
}
public class OrderProvisioned
{
public string OrderCode { get; set; }
public OrderProvisioned(string orderCode)
{
OrderCode = orderCode;
}
}
如何将 OrderProvisoned 和 OrderInvoiced 事件关联到与初始 OrderCreated 事件相同的 OrderState 实例,而不在我的事件中发送 Guid 并且只使用 ordercode 属性来关联它们?如果我运行此示例,如果同时发送了 OrderProvisioned 和 OrderInvoiced,我永远不会收到 OrderFinished 事件,但是如果我将 Guid 添加到事件中并根据该 Guid 关联它们,它就会正确执行。