我可以使用 WhenInjectedInto 为 ninject 指定多个参数吗?
Can I specify multiple parameters using WhenInjectedInto for ninject?
设置 ninject 绑定时,我使用 .ToMethod 为特定连接字符串指定特定参数,并使用 WhenInjectedInto 方法将绑定限制为特定类型:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup)()
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of MFUtility)()
我的问题是,我可以这样做吗:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup, MFUtility)()
一次为绑定指定多个目的地,而不是多行?
不是开箱即用的。但是您可以创建自己的 When(Func<IRequest,bool>)
扩展名来实现这一点。例如:
public static class WhenExtensions
{
public static IBindingInNamedWithOrOnSyntax<T> WhenInjectedInto<T>(
this IBindingWhenSyntax<T> syntax, params Type[] types)
{
var conditions = ComputeMatchConditions(syntax, types).ToArray();
return syntax.When(request => conditions.Any(condition => condition(request)));
}
private static IEnumerable<Func<IRequest, bool>> ComputeMatchConditions<T>(
IBindingWhenSyntax<T> syntax, Type[] types)
{
foreach (Type type in types)
{
syntax.WhenInjectedInto(type);
yield return syntax.BindingConfiguration.Condition;
}
}
}
像这样使用:
public class Test
{
[Fact]
public void Foo()
{
var kernel = new StandardKernel();
kernel.Bind<string>().ToConstant("Hello")
.WhenInjectedInto(typeof(SomeTypeA), typeof(SomeTypeB));
kernel.Bind<string>().ToConstant("Goodbye")
.WhenInjectedInto<SomeTypeC>();
kernel.Get<SomeTypeA>().S.Should().Be("Hello");
kernel.Get<SomeTypeB>().S.Should().Be("Hello");
kernel.Get<SomeTypeC>().S.Should().Be("Goodbye");
}
}
public abstract class SomeType
{
public string S { get; private set; }
protected SomeType(string s)
{
S = s;
}
}
public class SomeTypeA : SomeType
{
public SomeTypeA(string s) : base(s) { }
}
public class SomeTypeB : SomeType
{
public SomeTypeB(string s) : base(s) { }
}
public class SomeTypeC : SomeType
{
public SomeTypeC(string s) : base(s) { }
}
请注意,ComputeMatchConditions
是一种 hack,因为它依赖于 ninject 内部结构。如果这些(WhenInjectedInto
的实现)发生变化,那么这可能会停止工作。当然,您可以自由提供自己的替代实现。您也可以从 WhenInjectedInto
复制代码,参见:BindingConfigurationBuilder.cs method WhenInjectedInto(Type parent)
设置 ninject 绑定时,我使用 .ToMethod 为特定连接字符串指定特定参数,并使用 WhenInjectedInto 方法将绑定限制为特定类型:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup)()
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of MFUtility)()
我的问题是,我可以这样做吗:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup, MFUtility)()
一次为绑定指定多个目的地,而不是多行?
不是开箱即用的。但是您可以创建自己的 When(Func<IRequest,bool>)
扩展名来实现这一点。例如:
public static class WhenExtensions
{
public static IBindingInNamedWithOrOnSyntax<T> WhenInjectedInto<T>(
this IBindingWhenSyntax<T> syntax, params Type[] types)
{
var conditions = ComputeMatchConditions(syntax, types).ToArray();
return syntax.When(request => conditions.Any(condition => condition(request)));
}
private static IEnumerable<Func<IRequest, bool>> ComputeMatchConditions<T>(
IBindingWhenSyntax<T> syntax, Type[] types)
{
foreach (Type type in types)
{
syntax.WhenInjectedInto(type);
yield return syntax.BindingConfiguration.Condition;
}
}
}
像这样使用:
public class Test
{
[Fact]
public void Foo()
{
var kernel = new StandardKernel();
kernel.Bind<string>().ToConstant("Hello")
.WhenInjectedInto(typeof(SomeTypeA), typeof(SomeTypeB));
kernel.Bind<string>().ToConstant("Goodbye")
.WhenInjectedInto<SomeTypeC>();
kernel.Get<SomeTypeA>().S.Should().Be("Hello");
kernel.Get<SomeTypeB>().S.Should().Be("Hello");
kernel.Get<SomeTypeC>().S.Should().Be("Goodbye");
}
}
public abstract class SomeType
{
public string S { get; private set; }
protected SomeType(string s)
{
S = s;
}
}
public class SomeTypeA : SomeType
{
public SomeTypeA(string s) : base(s) { }
}
public class SomeTypeB : SomeType
{
public SomeTypeB(string s) : base(s) { }
}
public class SomeTypeC : SomeType
{
public SomeTypeC(string s) : base(s) { }
}
请注意,ComputeMatchConditions
是一种 hack,因为它依赖于 ninject 内部结构。如果这些(WhenInjectedInto
的实现)发生变化,那么这可能会停止工作。当然,您可以自由提供自己的替代实现。您也可以从 WhenInjectedInto
复制代码,参见:BindingConfigurationBuilder.cs method WhenInjectedInto(Type parent)