gpt4 book ai didi

masstransit - 如何在不使用 Guid 的情况下关联大众运输状态机中的事件?

转载 作者:行者123 更新时间:2023-12-03 16:42:56 27 4
gpt4 key购买 nike

我在 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 实例,而不在我的事件中发送 Guids 并且只使用 ordercode 属性来关联它们?
如果我运行这个例子,如果 OrderProvisioned 和 OrderInvoiced 都被发送,我永远不会得到 OrderFinished 事件,但是如果我将 Guid 添加到事件并根据该 Guid 关联它们,它会正确执行。

最佳答案

我解决了它,显然您必须在状态机实例上明确设置自定义相关性,我希望大众运输会为我做到这一点。
所以这是初始状态的改编代码:

            Initially(
When(OrderCreated)
.Then(context => context.Instance.OrderCode = context.Data.OrderCode)
.Then(context => Console.WriteLine("Order created"))
.TransitionTo(Created));

也使用 saga 工厂工作,但我认为您可以删除 selectid 逻辑,因为 saga 工厂否决了这一点,但我得到了一个异常(exception)。
        Event(() => OrderCreated,
x =>
{
x.CorrelateBy(order => order.OrderCode, ctx => ctx.Message.OrderCode);
x.InsertOnInitial = true;
x.SetSagaFactory(context => new OrderState
{
CorrelationId = NewId.NextGuid(),
OrderCode = context.Message.OrderCode
});
x.SelectId(context => NewId.NextGuid());
});

关于masstransit - 如何在不使用 Guid 的情况下关联大众运输状态机中的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com