使用Ninject进行部分注入? [英] Partial injection with Ninject?

查看:74
本文介绍了使用Ninject进行部分注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在构造函数中具有bool参数的现有类上实现Ninject

I am trying to implement Ninject on an existing class that have a bool parameter in the constructor

public MyClass(bool val) //[OPTION 1: Current]
{
    //I Called static function to do something -> I want to inject this
    ...
    if(val){ ... } else{ ... }
    ...
}

我想更改注入ISomething的逻辑....ctor如下所示:

I want to change the logic to inject ISomething.... the ctor will look like:

public MyClass(ISomething something, bool val) //[OPTION 2: Desired]
{
    something.DoSomething();
    ...
    if(val){ ... } else{ ... }
    ...
}

我还使用了一个一次性的类,称为该类:[例如.使用过OPTION 1->尝试转换为OPTION 2]

Also I used a disposable class that, called this one: [eg. used OPTION 1 -> trying to convert to OPTION 2]

public MyMaster(bool val)
{
    this.myclass = new MyClass(val); //-> now this has to be injected, how???
}

我在using上下文中用真或假参数实现了几次,例如:使用过OPTION 1->尝试转换为OPTION 2]

I implemented that a couple of times in a using context with true or false parameters, like this: [eg. used OPTION 1 -> trying to convert to OPTION 2]

using(var master = new MyMaster(true)){...} //-> This need to be refactored or injected, how???
//...
using(var master = new MyMaster(false)){...} //-> This need to be refactored or injected, how???

我在这里受到限制...因为我需要注入一个值而不注入另一个值.也许实现运行时注入?是否还需要将其包装在using语句中?

I am kind of blocked here... because I need to inject one value and not inject the other. Maybe implementing runtime injection? Also wrapping it in a using statement if it is needed?

注意:这是在对注入一无所知的类库中完成的(Ninject在后面的层中)

NOTE: This is done in a class library that doesn't know anything about injection (Ninject is in later layers)

推荐答案

我希望我已正确理解了这个问题-您需要基于传递给MyMaster构造函数的boolMyClass避免使用new,而是让Ninject为您提供它,对吗?

I hope I have understood the question correctly - you need a MyClass based on the bool passed to the MyMaster constructor, and you want to avoid using new but instead have Ninject supply it for you, is that right?

为此,您可以使用抽象工厂. (注意:正如Simon Whitehead指出的那样,您可以使用Ninject工厂扩展名,但是为了清楚起见,这里我将做一个明确的扩展名).

For this, you can use an abstract factory. (Note: You can, as Simon Whitehead pointed out, use the Ninject factory extensions for this but I will just do an explicit one here for clarity)

这些接口/类是在您的类库中定义的,而不是在合成根目录中是.

These interfaces/classes are defined in your class library, not in the composition root.

public interface IMyClassFactory
{
   IMyClass Create(bool val);
}

public interface IMyClass
{
   //whatever methods, etc, it's supposed to have.
}

public class MyClass:IMyClass
{
   public MyClass(bool val)
    {
       //do something with val here in the ctor
    }
}

该类是在您的合成词根中定义的

This class is defined in your composition root

public class MyClassFactory:IMyClassFactory
{
   readonly IKernel _kernel;
   public MyClassFactory(IKernel kernel)
   {
      _kernel=kernel;
   }
   public IMyClass Create(bool val)
   {
      return _kernel.Get<IMyClass>(new ConstructorArgument("val",val);
   }
}

然后,无论您在何处进行常规绑定,都可以在组合根目录中绑定类:

Then you bind the classes in your composition root, wherever you do your usual bindings:

kernel.Bind<IMyClassFactory>().To<MyClassFactory>();
kernel.Bind<IMyClass>().To<MyClass>();

您的MyMaster构造函数现在如下所示:

and your MyMaster constructor now looks like:

readonly IMyClass myclass;
public MyMaster(bool val,IMyClassFactory factory)
{
    this.myclass = factory.Create(val); 
}

一旦您了解了它的工作原理,就可以将其丢弃,并按照建议使用Ninject Factory扩展,因为它们可以节省您自己编写工厂类的时间:) (例如此处)

Once you get this working, and understand how it works, you can throw it away and use the Ninject Factory extensions as suggested, as they save you having to write the factory classes yourself :) (Example here)

这篇关于使用Ninject进行部分注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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