【发布时间】:2016-06-03 06:52:21
【问题描述】:
由于 Akka.net 系统只返回一个 Actor 的引用 (IActorRef),并且您只能通过 Tell() 和 Ask() 与 Actor 交互,其中参数不是强类型的,只是类型为 object base ...我脑海中出现了一些问题。
如何确保在编译时与正确的 Actor 类型“对话”? (例如,如果您在类的构造函数中仅接收 IActorRef 而不是具体的接口契约或通过对 Actor System 的请求,那么您不知道 Actor 能够处理该消息) 您如何确保 Actor 属于特定类型以及如何知道您为该类型选择了正确的消息?
尽管松散耦合很好,但它的缺点多于“TellAnything”对象契约方法的优点(从我的角度来看),Microsoft Orleans Grains 在我看来更适合这里,因为你可以有一个显式的接口.我是否错过了 Akka.net 提供的具有严肃合同的功能?
具有显式界面的 Microsoft Orleans:
public interface IHello : Orleans.IGrainWithIntegerKey
{
Task<string> SayHello(string greeting);
}
public class HelloGrain : Orleans.Grain, IHello
{
Task<string> SayHello(string greeting)
{
return Task.FromResult($"You said: '{greeting}', I say: Hello!");
}
}
// Get a reference to the IHello grain with id '0'.
var friend = GrainClient.GrainFactory.GetGrain<IHello>(0);
// Send a greeting to the grain and await the response.
Console.WriteLine(await friend.SayHello("Good morning, my friend!"));
Akka.net 只抱怨 Tell and Ask(带有对象参数):
Akka.net 中是否有类似的东西允许显式合同?我知道有支持在 Java 中使用 Akka 的代理,但 Akka.net 似乎在这里没有可比性。
MyActorSystem = ActorSystem.Create("MyActorSystem");
// create top-level actors within the actor system
Props consoleWriterProps = Props.Create<ConsoleWriterActor>();
IActorRef consoleWriterActor = MyActorSystem.ActorOf(consoleWriterProps, "consoleWriterActor");
// now you can tell everything to consoleWriterActor at compile time (even though this Actor only understands a specific message)
// begin processing
consoleReaderActor.Tell(ConsoleReaderActor.StartCommand);
如何保证“接口”的客户端只使用正确的命令?
如果 Akka.net 有这样的东西就好了:
Props consoleWriterProps = Props.Create<ConsoleWriterActor>();
IConsoleWriter consoleWriterActor = MyActorSystem.ActorOf<IConsoleWriter>(consoleWriterProps, "consoleWriterActor");
consoleReaderActor.MyExplicitCallThatAllowsOnlyStartCommandType(ConsoleReaderActor.StartCommand);
【问题讨论】:
标签: interface compile-time type-safety akka.net