Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
comman的C#泛型类型推断_C#_.net_Easynetq - Fatal编程技术网

comman的C#泛型类型推断

comman的C#泛型类型推断,c#,.net,easynetq,C#,.net,Easynetq,我必须使用我无法控制的特定API: TResponse Request<TRequest, TResponse>(TRequest request) where TRequest : class where TResponse : class; treresponse请求(TRequest请求) 特雷奎斯特:课堂在哪里 其中响应:类; 这是EasyNetQ库中RabbitMq的RPC包装 我想有一些方法来定义TRequest到TreResponse的关系,这样

我必须使用我无法控制的特定API:

TResponse Request<TRequest, TResponse>(TRequest request) 
    where TRequest : class 
    where TResponse : class;
treresponse请求(TRequest请求)
特雷奎斯特:课堂在哪里
其中响应:类;
这是EasyNetQ库中RabbitMq的RPC包装

我想有一些方法来定义TRequest到TreResponse的关系,这样我就不必像这样在每次调用中手动匹配它们:

this._client.Request<CancelPaymentByMerchantRequest, CancelPaymentByMerchantResponse>(request);
this.\u客户请求(Request);
我提出的解决方案是:

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; }
}
公共接口ICommand{}
公共类StartCommand:ICommand
{
公共int Id{get;set;}
公共字符串Args{get;set;}
}
公共类StartResponse
{
公共bool成功{get;set;}
公共字符串错误{get;set;}
}
以及它的消费者:

 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;
        }
公共响应发送(ICommand有效负载)
特雷奎斯特:课堂在哪里
在哪里响应:类
{
var result=client.Request((TRequest)负载);
返回结果;
}
是否可以在ICommand标记接口上删除TRequest类型

我还担心与拥有marker接口相关的额外成本,然后在实际的库调用中直接从该接口进行转换


任何建议都将不胜感激。

实现这一点的最简单方法就是在
Send
周围添加一个抽象,每个
TRequest
类型一次:

public SpecificResponse Send(SpecificRequest payload)
{
    return Send<SpecificRequest, SpecificResponse>(payload);
}
公共特定响应发送(特定请求负载)
{
返回发送(有效载荷);
}
您必须为
Send
per
TRequest、treresponse
对定义一个重载,但应用程序的其余部分可以使用这些“强类型”版本,以避免在任何地方重复关系

如何编排这取决于您,但我更希望每个请求/响应类型都有一个接口/类,该接口/类充当通用层的中介(或者一个带有一系列契约的大型接口也可以)