我很可能会在这里丢失一些明显的东西;但如果我有以下几点:有没有办法避免必须在C#中链接泛型类型参数?
public interface IRequest<out T>
where T : class
{
T Request
{
get;
}
}
和:
public interface IResponse<out T>
where T : class
{
T Response
{
get;
}
}
然后它使用这些接口,泛型类型参数的第三接口:
public interface IResponder<in TRequest, TRequestValue, out TResponse, TResponseValue>
where TRequest : IRequest<TRequestValue>
where TResponse : IResponse<TResponseValue>
where TRequestValue : class
where TResponseValue : class
{
TResponse Query(TRequest request);
}
有没有办法避免必须通过TRequestValue
和TResponseValue
作为通用参数到IResponder
(并因此避免必须复制相关的约束) ?
基本上我宁愿有类似下面的内容到的泛型类型参数的数量减少到IResponder
(我知道这不会在此刻编译):
public interface IResponder<in TRequest, out TResponse>
where TRequest : IRequest
where TResponse : IResponse
{
TResponse Query(TRequest request);
}
唯一的办法我可以认为这样做是编写两个非通用接口(IRequest
和IResponse
),然后使IRequest<T>
和IResponse<T>
实现这些,但这在某种程度上似乎是错误的。
我完全知道我可能因为我在这里做事情而遇到了这个问题,所以请随时提出我做错了什么!
注意:整个Request,Response,Responder示例实际上并不是我正在编写的代码,而是尝试支持我的问题的示例。
干杯柯克。我认为你说得很好;再看一下我的设计,我不认为这两个接口实际上是通用的,所以我会在我们讲话时重新审视它。 – Dougc 2010-08-02 08:24:00