获取ninject工厂扩展名以允许将工厂参数传递给依赖项 [英] Get ninject factory extension to allow factory parameters be passed to dependencies

查看:92
本文介绍了获取ninject工厂扩展名以允许将工厂参数传递给依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ninject Factory扩展,您可以自动生成工厂,并让工厂将参数传递给类的构造函数.以下测试通过:

Using the Ninject Factory extension, you can automatically generate factories, and let the factory pass parameters to the class' constructor. The following test passes:

public interface IBar
{
    int Foo { get; }
}

public class Bar : IBar
{
    int _foo;
    public Bar(int foo) { _foo = foo; }
    public int Foo { get { return _foo; } }
}

public interface IBarFactory
{
    IBar CreateTest(int foo);
}

[Test]
public void ExecuteTest()
{
    var kernel = new StandardKernel();
    kernel.Bind<IBar>().To<Bar>();
    kernel.Bind<IBarFactory>().ToFactory();
    var factory = kernel.Get<IBarFactory>();
    var actual = factory.CreateTest(42);
    Assert.That(actual, Is.InstanceOf<Bar>());
}

但是,在我的特定问题中,我希望将工厂参数传递给正在构造的类的依赖项,而不是类本身,如下所示:

However, in my particular problem, I would like it to pass the factory arguments to a dependency of the class I'm constructing, not the class itself, like this:

public interface IBarContainer
{
    IBar Bar { get; }
}

public class BarContainer : IBarContainer
{
    IBar _bar;
    public BarContainer(IBar bar) { _bar = bar; }
    public IBar Bar { get { return _bar; } }
}

public interface ITestContainerFactory
{
    IBarContainer CreateTestContainer(int foo);
}

[Test]
public void ExecuteTestContainer()
{
    var kernel = new StandardKernel();
    kernel.Bind<IBar>().To<Bar>();
    kernel.Bind<IBarContainer>().To<BarContainer>();
    kernel.Bind<ITestContainerFactory>().ToFactory();
    var factory = kernel.Get<ITestContainerFactory>();
    var actual = factory.CreateTestContainer(42);
    Assert.That(actual.Bar, Is.InstanceOf<Bar>());
}

此测试失败,并显示以下消息:

This test fails with the message:

Ninject.ActivationException:激活int时出错,没有匹配项 绑定是可用的,并且类型不是自绑定的.激活 路径:
3)将依赖int注入到Bar类型的构造函数的参数foo
2)将依赖项IBar注入到BarContainer类型的构造函数的参数栏中
1)请求IBarContainer

Ninject.ActivationException : Error activating int No matching bindings are available, and the type is not self-bindable. Activation path:
3) Injection of dependency int into parameter foo of constructor of type Bar
2) Injection of dependency IBar into parameter bar of constructor of type BarContainer
1) Request for IBarContainer

是否有一种方法可以使工厂弄清楚在解决"Bar"依赖项时是否要使用传递的"foo"参数?

Is there a way to make the factory figure out that I want to use the passed 'foo' argument when resolving the 'Bar' dependency?

推荐答案

从Ninject.Extensions.Factory的3.2.0版本开始,使用以下方法原生支持:

From version 3.2.0 of Ninject.Extensions.Factory this is supported natively, using:

Ninject.Extensions.Factory.TypeMatchingArgumentInheritanceInstanceProvider

您可以像这样使用它:

kernel.Bind<IxxxFactory>()
   .ToFactory(() => new TypeMatchingArgumentInheritanceInstanceProvider());

如果您查看先前的答案.

If you look at the source code, it's based on the previous answer.

这篇关于获取ninject工厂扩展名以允许将工厂参数传递给依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆