如何前进到下一个状态,而不对收到的每条消息重复相同的条件评估?



我有一个参与者需要从各种来源收集数据,然后才能继续处理。目前,我对收到的每条消息重复相同的条件评估:

private Foo foo;
private Bar bar;
private void DoSomething(DoSomething message)
{
this.BecomeWaitingForInputData();
this.GetFoo(message.FooId); /* sends message */
this.GetBar(message.BarId); /* sends message */
}
private void BecomeWaitingForInputData()
{
this.Become(() =>
{
this.Receive<Foo>((message) =>
{
this.CollectFoo(message); /* assigns to foo */
this.ProcessInputDataIfReady();
});
this.Receive<Bar>((message) =>
{
this.CollectBar(message); /* assigns to bar */
this.ProcessInputDataIfReady();
});
});
}
private void ProcessInputDataIfReady()
{
if (this.foo == null || this.bar == null) return;
/* ... */
this.BecomeWaitingForProcessResults();
}

有没有办法避免重复this.ProcessInputDataIfReady();,或者这是最好的编写方法?我能找到的大多数示例(例如这个)都是用 Scala 编写的,它似乎允许各种花哨的东西及其语法,而且我在 C# 中并没有真正看到等效项。

您可以在Akka.NET -docs- -source-中使用FSM<>类,它允许您在处理每个事件时返回下一个行为。

目前编写的ReceiveActor不允许 Scala 拥有的很好的AndThen类型的运算符,所以如果你想坚持这个设计,据我所知,你现在可能是最不痛苦的方式。

相关内容

最新更新