Search code examples
c#.net-6.0protobuf-net

Protobuf-net.Grpc generic services


I've got a few gRPC services that all share a common method and was hoping to be able to do something like this:

[Service]
public interface IVehicleReloader<T> where T : IVehicle
{
    public ValueTask<T> ReloadInstance(ReloadInstanceRequest path);
}

[Service]
public interface ICarService : IVehicleReloader<Car>
{
...
}

[Service]
public interface IMotorbike : IVehicleReloader<Motorbike>
{
...
}

But this won't even compile, it straight up says The gRPC service cannot be generic.

Is there any other way of achieving this, short of having to declare the method on each of the interfaces?


Solution

  • This ties into some topics that have been discussed in the repo about flattening services. The following is implemented via PR 206 (not by me - contributed from a user), and will be deployed shortly:

    [SubService] // naming is hard
    public interface IVehicleReloader<T> where T : IVehicle
    {
        public ValueTask<T> ReloadInstance(ReloadInstanceRequest path);
    }
    
    [Service]
    public interface ICarService : IVehicleReloader<Car>
    {
    ...
    }
    
    [Service]
    public interface IMotorbike : IVehicleReloader<Motorbike>
    {
    ...
    }
    

    Here the [SubService] means that IVehicleReloader<T> is not treated as a service itself, but instead: those members are folded into the [Service] types where they are discovered, ergo: ReloadInstance appears directly in ICarService and IMotorbike, and is routed appropriately.

    I'd love to get this shipped - it is a matter of time, priorities, etc.