[英]Access grain without interface
I'm planning my IoT system and comparing Akka.NET with Orleans. 我正在计划我的物联网系统,并将Akka.NET与Orleans进行比较。 Generally Orleans API is more close to .NET standards but I'm wondering is there any other way to communicate with Grain?
通常,奥尔良API更接近.NET标准,但我想知道还有其他与Grain进行通信的方法吗? In documentation I see that we define interface like
在文档中,我看到我们定义了接口
public interface IDevice : IGrainWithGuidKey
{
Task TurnOn(TurnOnCommand command);
Task TurnOff(TurnOffCommand command);
}
And use it like this 像这样使用
IDevice device = GrainFactory.GetGrain<IDevice>(id);
This is fine when we have actors that have very strict interface but in home we have a lot of devices and I don't want to define 100x interfaces for each of them. 当我们的演员具有非常严格的界面但在家里我们有很多设备并且我不想为每个设备定义100x接口时,这很好。 I would prefer to each of them have set of handlers for commands that it can handle and client could invoke
我希望他们每个人都有一套可以处理的命令处理程序,客户端可以调用这些处理程序
device.Execute(new TurnOnCommand())
without defining interface for each. 无需为每个接口定义接口。 Is this possible in Orleans?
在奥尔良有可能吗?
You can define an interface which accepts a base class/interface, such as ICommandProcessor<TCommand>
and implement multiple versions of that, so you might have IMyDeviceGrain
implementing ICommandProcessor<TurnOnCommand>, ICommandProcessor<TurnOffCommand>
, and so on. 您可以定义一个接受基类/接口的接口,例如
ICommandProcessor<TCommand>
并实现该接口的多个版本,因此您可以让IMyDeviceGrain
实现ICommandProcessor<TurnOnCommand>, ICommandProcessor<TurnOffCommand>
等。
In that case, you might define ICommandProcessor<TCommand>
as: 在这种情况下,可以将
ICommandProcessor<TCommand>
定义为:
public interface ICommandProcessor<TCommand>
{
Task Process(TCommand command);
}
The grain interface might look like: 谷物界面可能类似于:
public class IDeviceGrain :
IGrainWithStringKey,
ICommandProcessor<TurnOnCommand>,
ICommandProcessor<TurnOffCommand>
{
}
The grain class could then look like: 谷物类如下所示:
public class DeviceGrain : Grain, IDeviceGrain
{
Task Process(TurnOnCommand command) { /* turn on */ }
Task Process(TurnOffCommand command) { /* turn off */ }
}
I've also seen people implement this by having a grain which has a single Task Process(object command)
call which internally used dynamic
to dispatch based on the type of command
. 我还看到人们通过具有单个
Task Process(object command)
调用的谷物来实现此目的,该谷物在内部根据command
类型在内部使用dynamic
进行调度。
I hope that helps. 希望对您有所帮助。 Please let me know if there are any points which you would like clarified.
如果您有任何要澄清的地方,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.