Home > Software engineering >  Protobuf-net.Grpc generic services
Protobuf-net.Grpc generic services

Time:09-12

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?

CodePudding user response:

Right now, so, basically. However, this ties into some topics that have been discussed in the repo about flattening services. The following is not currently implemented, but consider:

[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.

  • Related