本文介绍了我可以使用WhenInjectedInto来为ninject指定多个参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置我的ninject绑定时,我使用.ToMethod为特定的连接字符串指定特定的参数,并使用WhenInjectedInto方法将绑定限制为特定的类型:

When setting up my ninject bindings, I'm using the .ToMethod to specify particular parameters for specific connectionstrings, and the WhenInjectedInto method to constrain the binding to specific types:

        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)()

我的问题是,我可以做这样的事情吗?

My question is, can I do something like this:

        Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup, MFUtility)()

一次为绑定指定一个以上的目的地,而不是多行?

Specifying more than one destination for the binding at once, rather than having multiple lines?

推荐答案

不是开箱即用的.但是您可以为When(Func<IRequest,bool>)创建自己的扩展名,而扩展名确实可以做到这一点.例如:

Not out of the box. But you can create your own extension to When(Func<IRequest,bool>) which does exactly that. For example:

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方法WhenInjectedInto(Type parent)

Note that ComputeMatchConditions is kind of a hack because it relies on ninject internals.. if these (the implementation of WhenInjectedInto) change then this may stop to work. Of course you're free to provide your own alternative implementation. You could also copy the code from WhenInjectedInto, see: BindingConfigurationBuilder.cs method WhenInjectedInto(Type parent)

这篇关于我可以使用WhenInjectedInto来为ninject指定多个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 22:11