如何在proto文件中表示接口实现



我正在使用protogen工具从.proto文件生成c#类。我想知道是否有可能在原型文件中表示接口实现。例如,有没有任何方法可以在proto文件中表示以下内容。

interface ILog
{
}
class ConsoleLog: ILog
{
}

.proto没有接口的概念,除非你计算服务(无论如何,protogen都不涉及服务(。

如果你想在本地添加一些东西,代码都是C#,但我的建议是简单地使用"分部类",并在另一个代码文件中添加所有接口方面。Protogen总是发出分部类。

TL;DR-创建一个事件包装

这有点晚了,但我想我会发布一个想法。这不是在.proto文件上创建接口的用例,只是解决了我认为与本文有关的问题(我最初也有同样的想法(。我想处理通用事件,但这些事件与原型生成的类绑定在一起。

TempEvent.proto

message TempEvent {
int32 deviceId = 1;
float humidity = 2;
float temperature = 3;
}

从事件队列中获取消费者类(传感器读数(:

Service service;
TempEvent event = queue.remove();
((EventService)service).process(new EventWrapper(event));

一旦数据被反序列化,只需创建一个实现Event的简单包装器。

public interface Event<T> {
T getEvent();
}
public class EventWrapper<T> implements Event {
private T event;
public EventWrapper(T eventType) {
this.event = eventType;
}
public T getEvent() {
return this.event;
}
}

最新更新