假设我有一个服务接口: public interface IFooService{ void DoSomething();} 并且该服务的具体实现是通用的: public class FooServiceTRequestingClass : IFooService{ public virtual void DoSomething() { }} 我还有一些需
public interface IFooService
{
void DoSomething();
}
并且该服务的具体实现是通用的:
public class FooService<TRequestingClass> : IFooService
{
public virtual void DoSomething() { }
}
我还有一些需要IFooService实例的其他类:
public class Bar
{
private IFooService _fooService;
public Bar(IFooService fooService)
{
this._fooService = fooService;
}
}
我需要连接我的IoC容器,这样当创建Bar时,它会传递一个FooService< Bar>的构造函数参数.还有很多其他课程就像Bar一样.每个人都可能需要一个FooService实例< TRequestingClass>传递给他们,其中TRequestingClass是需要IFooService实例的类的类型.我不需要向IFooService的消费者公开这个怪癖.他们应该关心的是他们可以调用他们传递的IFooService的方法.他们不应该知道他们传递的IFooService的具体实现需要任何特殊的构造.
FooService< T>的可接受的替代方案.将是一个非泛型类,在其构造函数中包含一个字符串参数,该类包含要为其创建的类的名称.即:
public class FooService : IFooService
{
public FooService(string requestingClassName) { }
}
如何通过这种方式连接我的IoC容器来构建依赖项?
如果你为什么我想要这样一个奇怪的结构而感到困惑,那么考虑当你得到一个用log4net.LogManager.GetLogger(typeof(SomeClass))创建的ILog时log4net如何工作得最好.我不想通过引用log4net来丢弃我的代码,所以我想编写一个简单的ILogger接口,并用这样的方式实现它:
public class GenericLogger<T> : ILogger
{
private readonly ILog log;
public GenericLogger()
{
this.log = log4net.LogManager.GetLogger(typeof(T));
}
public void Debug(object message)
{
this.log.Debug(message);
}
/* .... etc .... */
}
最简单的方法是创建ILogger< T>.接口:
public class ILogger<T> : ILogger { }
public class GenericLogger<T> : ILogger<T> { ... }
然后依靠泛型类型推断来获得正确的类型.例如,在Ninject中,您需要以下绑定:
Bind(typeof(ILogger<>)).To(typeof(GenericLogger<>));
那么您的消费类型将如下所示:
public class FooService : IFooService {
public FooService(ILogger<FooService> logger) { ... }
}
如果你坚决反对ILogger< T>界面,您可以做一些更有创意的事情,比如自定义提供程序,它会读取IContext以确定父类型.
public class GenericLogger : ILogger {
public class GenericLogger(Type type) { ... }
}
public class LoggerProvider : Provider<ILogger> {
public override ILogger CreateInstance(IContext context) {
return new GenericLogger(context.Target.Member.ReflectedType);
}
}
然后消费类型将如下工作:
public class FooService : IFooService {
public FooService(ILogger logger) { ... }
}
