[英]Hiding type parameters in implementation of interface
给定以下接口:
public interface IContext {
TOutput get<TInput, TOutput>(TInput command);
}
以及以下实现:
public class DbContext: IContext {}
public class Repository {
private readonly IContext _context;
public Repository(IContext context) {...}
public IDto get(int id) {
var data = _context.get<IThis, IThat>(id)
//map data to dto and return
}
}
由于我将IContext
依赖项传递到Repository
类中,因此我不需要接口上的类型参数,因为它会使我无法使用IContext的显式实现。 那种使接口静音的点对吗?
鉴于我在IContext
接口上没有类型参数的约束,我该如何实现IContext
以便可以在我的Repository
类中调用_context.get(...)
,而不是_context.get<IThis, IThat>(...)
?
因此,换句话说,我希望实现IContext
(在此示例中为DBContext
)来定义get()
的类型参数,以便在实际调用该方法时,调用者不需要了解任何有关类型参数的信息。被通过。
更新
我要解决的问题是允许将任何类型的IContext
传递到存储库中。 如果我在接口上具有类级别的类型参数,则只能使用IContext<TThis, TThat>
,这是不理想的。
如果IContext
对象具有可以接受任何对象作为输入并提供任何对象作为输出的get
函数是很重要的,那么,而不是泛型,您的接口方法应该简单地使用object
:
public interface IContext {
object get(object command);
}
(接口的实现者可能会暴露出强类型方法,并对该方法使用显式接口实现。
另一种可能性是,当您接受IContext
对象时,您的IContext
对象知道该方法的签名是什么。 在这种情况下,您要使该接口(而不是其方法)通用:
public interface IContext<TInput, TOutput> {
TOutput get(TInput command);
}
当然,这意味着您不可能拥有各种IContext
对象的各种对象的集合。 他们都需要共享一个签名。 接受或返回IContext
对象的方法将需要知道在编译时输入/输出应该是什么类型,或者它们本身需要是通用的。
附带说明一下,可能与您对接口的使用无关或不相关,您可以在通用参数上利用协方差和协方差,这可能会也可能不会帮助您使用接口:
public interface IContext<in TInput, out TOutput>
{
TOutput get(TInput command);
}
如果不了解接口的实际使用方式,目前尚不清楚哪种方法更好。
据我了解,您需要具体的输入以及通过接口的松散耦合。 考虑一下:
public interface ICommand {}
public interface IContextOutput {}
public interface IContext
{
IContextOutput Get(ICommand input);
}
public abstract class ContextBase<TInput, TOutput> : IContext
where TInput : ICommand
where TOutput : IContextOutput
{
IContextOutput IContext.Get (ICommand input)
{
if (input.GetType () != typeof(TInput))
{
throw new ArgumentOutOfRangeException("input");
}
return Get((TInput)input);
}
protected abstract TOutput Get(TInput command);
}
然后从抽象类派生所有上下文 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.