Search code examples
grpcgrpc-c#

Interface equivalent of C# in grpc proto file


I need to have common contract for set of proto files similar like interfaces in C#. Is there anything in grpc using which I can implement such functionality.

interface IMyInterfaceWithCommonMethods
{
    void MyCommonMethod1();
    void MyCommonMethod2();
}

interface IMyInterface2: IMyInterfaceWithCommonMethods
{
    void MyMethod2();
}


public class MyClass : IMyInterface2
{
    public void MyMethod2()
    {
        Console.WriteLine("MyMethod2");
    }

    public void MyCommonMethod1()
    {
        throw new NotImplementedException();
    }

    public void MyCommonMethod2()
    {
        throw new NotImplementedException();
    }
}

Expected code in grpc proto

service Myervice implements IMyInterfaceWithCommonMethods{
    rpc MyCommonMethod1(MyRequest1) returns (MyResponse1);
    rpc MyCommonMethod2(MyRequest1) returns (MyResponse1);
    rpc MyMethod2(MyRequest1) returns MyResponse1;
}

Solution

  • There is not.

    I understand why this would be desirable.

    I don't know why it isn't a facility of protocol buffers, perhaps:

    1. Simplicity
    2. Not all protobuf supporting languages support OO-like interfaces
    3. Service definitions can function like interfaces see Health Checking

    As an alternative, you could enforce this as organizational best practice and during code review.