I have to use specific API which I don't have control over:
TResponse Request<TRequest, TResponse>(TRequest request)
where TRequest : class
where TResponse : class;
This is an RPC wrapper for RabbitMq from EasyNetQ library
I would like to have some way to define TRequest to TResponse relation, so that I don't have to manually match them every call like this :
this._client.Request<CancelPaymentByMerchantRequest, CancelPaymentByMerchantResponse>(request);
The solution I come up with is :
public interface ICommand<TRequest, TResponse> { }
public class StartCommand : ICommand<StartCommand, StartResponse>
{
public int Id { get; set; }
public string Args { get; set; }
}
public class StartResponse
{
public bool Success { get; set; }
public string Error { get; set; }
}
And a consumer for it :
public TResponse Send<TRequest, TResponse>(ICommand<TRequest, TResponse> payload)
where TRequest : class
where TResponse : class
{
var result = client.Request<TRequest, TResponse>((TRequest)payload);
return result;
}
Is it possible to get rid of TRequest type on ICommand marker interface?
I am also worried about extra costs related to having marker interface, and then direct cast from it in the actual library call.
Any advice would be appreciated.