我需要更多Ninject实用示例 [英] I need more Ninject practical examples

查看:93
本文介绍了我需要更多Ninject实用示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我使用了swiftsuspenders,它是一个动作脚本3 IoC控制器.基本上,switfsuspender的第一个版本与Ninject内核类似,称为注射器".

In the past, I used swiftsuspenders that is an actionscript 3 IoC controller. Basically the first version of switfsuspender had something similar to the Ninject kernel that was called injector.

如果我想创建一个应用程序注入器(假设要在整个应用程序中使用最相关的映射),则必须将注入器本身注入到应用程序类中.

If I wanted to create an application injector (with let's say the most relevant mappings to be used throughout the application), I had to inject the injector itself in the application classes.

我现在想知道在应用程序的几个类中使用kernel.get<>的做法是什么.我应该注入内核本身吗?

I am wondering now what is the practice to use kernel.get<> among several classes in the application. Should I inject the kernel itself?

我个人更愿意使用kernel.inject,但是如果我可以做kernel.inject,我真的很可能会手动注入依赖项,这可能会更好(亲亲).

Personally I'd rather use kernel.inject but if I can do kernel.inject I can really likely inject the dependencies manually, which is probably better (kiss).

测试用例很好,但是它们与实际的实际问题相去甚远,所以希望您能帮助我阐明这一点.谢谢.

Test cases are nice, but they are far from the real practical issues, so I hope you can help me to clarify this point. Thank you.

我注意到有些人谈论根容器",看来这是我要寻找的概念.我应该如何设置一个根容器,并让其他应用程序类知道它?

I noticed that some people talk about "root container", It seems like it is the concept I am looking for. How should I setup a root container and let the other application classes know it?

Edit2示例代码(请原谅错误,仅出于示例目的):

Edit2 Sample code (please forgive errors, it is just for example sake):

class SomeClass
{
    public SomeClass()
    {
        Command cmd = new Command();

        cmd.execute();
    }

}

class SomeOtherClass:ISomeOtherClass
{
 public void allright()
 {
    //right
 }
}

class Command
{
   ISomeOtherClass dependency;

   void execute()
   {
    dependency.allright();
   }

}


Program.Main()
{
    IKernel kernel = new StandardKernel();

    kernel.Bind<SomeClass>().ToSelf().InSingletonScope();
    kernel.Bind<ISomeOtherClass>().To<SomeOtherClass>();

    SomeClass sc = kernel.Get<SomeClass>();
}

我尚未进行测试,因为我仍在解决一些初始化问题,但是我的问题是,命令类如何知道SomeOtherClass?我目前的假设是在SomeClass中注入内核并使用方法Inject.

I did not test this yet, because I am still fighting with some initialization issues, but my question is, how can the command class know about SomeOtherClass? My current hypothesis is to inject the kernel in SomeClass and use the method Inject.

推荐答案

以您的示例为例,很明显,SomeClass并不是在考虑控制反转"的情况下构建的.提示是它对Command有依赖关系,但是对该依赖关系的控制在SomeClass本身内部进行. (Command cmd = new Command();)

Looking at your example, it is clear that SomeClass is not built with Inversion of Control in mind; the tip-off being that it has a dependency on Command, but control of that dependency is maintained inside SomeClass itself. (Command cmd = new Command();)

反转该依赖项的控件,您将需要一种将该依赖项注入SomeClass的方法.正如Remo Gloor 指出的所示,使用Ninject进行此操作的标准方法是通过构造函数.

To invert the control of that dependency, you would need to have a way to inject that dependency into SomeClass. As Remo Gloor has indicated, the standard way of doing that with Ninject is through the constructor.

为此,您可以将SomeClass更改为以下内容:

To do so, you might change SomeClass to something like this:

class SomeClass
{
    private ICommand _command;

    public SomeClass(ICommand injectedCommand)
    {
        _command = injectedCommand;
        _command.execute();
    }

}

同样,您将需要Command公布其依赖项:

Likewise you would need your Command to advertise its dependency:

class Command
{
   private ISomeOtherClass _dependency;

   public Command(ISomeOtherClass injectedSomeOtherClass)
   {
        _dependency = injectedSomeOtherClass;
   {

   void execute()
   {
      _dependency.allright();
   }
}

然后,您将在内核中注册Command绑定,也许像这样:

Then you would register your Command binding in the kernel, perhaps like:

Program.Main()
{
    IKernel kernel = new StandardKernel();

    kernel.Bind<SomeClass>().ToSelf().InSingletonScope();
    kernel.Bind<ICommand>().To<Command>();
    kernel.Bind<ISomeOtherClass>().To<SomeOtherClass>();

    SomeClass sc = kernel.Get<SomeClass>();
}

然后,内核将能够遍历依赖链并将其全部注入.

The kernel would then be able to walk the dependency chain and inject them all.

这篇关于我需要更多Ninject实用示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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