簡體   English   中英

Ninject可能將上下文綁定應用於綁定組嗎?

[英]Ninject possible apply contextual binding to group of bindings?

是否可以將上下文綁定應用於一組綁定?

基本上,我想將相同的上下文綁定應用於多個綁定,在這種情況下,在模塊中:

public class MyNinjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<IType1>().To<MyFirstType>();
        Bind<IType2>().To<MySecondType>();
        Bind<IType3>().To<MyThirdType>();
        // ...and so on

        // here I want to apply a contextual binding to all bindings previously defined in this module
        // something like:
        foreach (var binding in this.Bindings)
            binding.WhenInjectedInto(typeof(MyClass));
    }
}

這有點棘手,我不知道如何執行。 我所知道的是,我們只能使用一種條件。 我擔心要覆蓋您以前的上下文,但是后來我意識到不可能“組合”這些條件。

因此,如果要執行此操作,請嘗試一下:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IType1>().To<MyFirstType>();
        Bind<IType2>().To<MySecondType>();
        Bind<IType3>().To<MyThirdType>();

        foreach (var binding in Bindings)
        {
            AssignedWhenInjectedInto<MyClass>(binding);
        }
    }

    public void AssignedWhenInjectedInto<T>(IBinding binding)
    {
        var parent = typeof (T);

        //Copied from Ninject's WhenInjectedInto<T> implementation
        if (parent.IsGenericTypeDefinition)
        {
            if (parent.IsInterface)
            {
                binding.BindingConfiguration.Condition = r =>
                    r.Target != null &&
                    r.Target.Member.ReflectedType.GetInterfaces().Any(i =>
                        i.IsGenericType &&
                        i.GetGenericTypeDefinition() == parent);
            }
            else
            {
                binding.BindingConfiguration.Condition = r =>
                    r.Target != null &&
                    r.Target.Member.ReflectedType.GetAllBaseTypes().Any(i =>
                        i.IsGenericType &&
                        i.GetGenericTypeDefinition() == parent);
            }
        }
        else
        {
            binding.BindingConfiguration.Condition = r => r.Target != null && parent.IsAssignableFrom(r.Target.Member.ReflectedType);
        }
    }
}

為此使用了以下測試:

[Test]
public void RewriteBindingConditionTest()
{
    _kernel = new StandardKernel(new MyModule());
    var instance = _kernel.Get<MyClass>();

    Assert.IsInstanceOf<MyFirstType>(instance.Type1);
    Assert.IsInstanceOf<MySecondType>(instance.Type2);
    Assert.IsInstanceOf<MyThirdType>(instance.Type3);

    try
    {
        _kernel.Get<MyOtherClass>();
    }
    catch (ActivationException)
    {
        Assert.Pass();
    }

    Assert.Fail("Bindings were injected into MyOtherClass while there was no configured binding for them.");
}

和輔助類:

public class MyClass
{
    public IType1 Type1 { get; set; }
    public IType2 Type2 { get; set; }
    public IType3 Type3 { get; set; }

    public MyClass(IType1 type1, IType2 type2, IType3 type3)
    {
        Type1 = type1;
        Type2 = type2;
        Type3 = type3;
    }
}

public class MyOtherClass
{
    public IType1 Type1 { get; set; }
    public IType2 Type2 { get; set; }
    public IType3 Type3 { get; set; }

    public MyOtherClass(IType1 type1, IType2 type2, IType3 type3)
    {
        Type1 = type1;
        Type2 = type2;
        Type3 = type3;
    }
}

public class MyThirdType : IType3
{
}

public class MySecondType : IType2
{
}

public class MyFirstType : IType1
{
}

public interface IType3
{
}

public interface IType2
{
}

public interface IType1
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM