Ninject基于参数类型的条件绑定

我正在使用工厂返回数据库:

Bind() .ToFactory(); public interface IDataSenderFactory { IDataSender CreateDataSender(Connection connection); } 

我有两种不同的datasender实现(WCF和远程处理),它们采用不同的类型:

 public abstract class Connection { public string ServerName { get; set; } } public class WcfConnection : Connection { // specificProperties etc. } public class RemotingConnection : Connection { // specificProperties etc. } 

我试图使用Ninject根据从参数传递的Connection类型绑定这些特定类型的数据集。 我尝试了下面的失败:

 Bind() .To() .When(a => a.Parameters.Single(b => b.Name == "connection") as RemotingConnection != null) 

我相信这是因为’。’只提供一个请求,我需要完整的上下文才能检索实际参数值并检查其类型。 我不知道该做什么,除了使用命名绑定,实际上实现工厂并将逻辑放在那里,即

 public IDataSender CreateDataSender(Connection connection) { if (connection.GetType() == typeof(WcfConnection)) { return resolutionRoot.Get("wcfdatasender", new ConstructorArgument("connection", connection)); } return resolutionRoot.Get("remotingdatasender", new ConstructorArgument("connection", connection)); } 

在查看Ninject源代码后,我发现以下内容:

  • a.Parameters.Single(b => b.Name == "connection")为您提供IParameter类型的IParameter ,而不是真实参数。

  • IParameter具有方法object GetValue(IContext context, ITarget target) ,它不需要null上下文参数(target可以为null)。

  • 我还没有找到任何方法从Request获取IContext(样本中的变量a)。

  • Context类没有无参数构造函数,因此我们无法创建新的Context。

为了使它工作,您可以创建虚拟IContext实现,如:

 public class DummyContext : IContext { public IKernel Kernel { get; private set; } public IRequest Request { get; private set; } public IBinding Binding { get; private set; } public IPlan Plan { get; set; } public ICollection Parameters { get; private set; } public Type[] GenericArguments { get; private set; } public bool HasInferredGenericArguments { get; private set; } public IProvider GetProvider() { return null; } public object GetScope() { return null; } public object Resolve() { return null; } } 

而不是使用它

 kernel.Bind() .To() .When( a => a.Parameters .Single( b => b.Name == "connection" ) .GetValue( new DummyContext(), a.Target ) as RemotingConnection != null ); 

如果有人可以发布一些关于从When()内部获取Context的信息会很好