我正在计划我的物联网系统并将Akka.NET与奥尔良进行比较。一般来说,Orleans API更接近.NET标准,但我想知道还有其他方式与Grain通信吗?在文档中,我看到我们定义了类似的接口
public interface IDevice : IGrainWithGuidKey
{
Task TurnOn(TurnOnCommand command);
Task TurnOff(TurnOffCommand command);
}
并像这样使用它
IDevice device = GrainFactory.GetGrain<IDevice>(id);
当我们拥有非常严格的接口的演员时,这很好,但在家里我们有很多设备,我不想为每个设备定义100x接口。我更愿意每个人都有一组处理程序,它们可以处理它们可以调用的命令
device.Execute(new TurnOnCommand())
没有为每个人定义界面。这在奥尔良有可能吗?
您可以定义一个接受基类/接口的接口,例如ICommandProcessor<TCommand>
并实现它的多个版本,因此您可能有IMyDeviceGrain
实现ICommandProcessor<TurnOnCommand>, ICommandProcessor<TurnOffCommand>
,依此类推。
在这种情况下,您可以将ICommandProcessor<TCommand>
定义为:
public interface ICommandProcessor<TCommand>
{
Task Process(TCommand command);
}
颗粒界面可能如下所示:
public class IDeviceGrain :
IGrainWithStringKey,
ICommandProcessor<TurnOnCommand>,
ICommandProcessor<TurnOffCommand>
{
}
粮食类可能看起来像:
public class DeviceGrain : Grain, IDeviceGrain
{
Task Process(TurnOnCommand command) { /* turn on */ }
Task Process(TurnOffCommand command) { /* turn off */ }
}
我也看到人们通过一个具有单个Task Process(object command)
调用的粒子实现这一点,内部使用dynamic
根据command
的类型进行调度。
我希望有所帮助。如果您有任何要澄清的地方,请告诉我。