什么是Ninject,什么时候使用它? [英] What is Ninject and when do you use it?

查看:141
本文介绍了什么是Ninject,什么时候使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个项目帮助几个朋友,并有一个使用Ninject类。我是相当新的C#和我不知道那是什么类是干什么的,这就是为什么我需要了解Ninject。谁能解释Ninject是什么,什么时候做一件使用它(例如带如果可能的话)? 。或者,如果你可以点一些链接,将是巨大的。



我想这个问题:的 Ninject教程/单证?但它并没有真正帮助像我这样的初学者。


解决方案

Ninject是.NET依赖注入,模式依赖注入的具体实现。<(控制模式的反转形式)/ p>

假设你有两个类 DbRepository 控制器

 类控制器{
私人DbRepository _repository;

// ...使用_repository
} $的一些方法B
$ B类DbRepository {
// ...这里的一些经营业务逻辑...
}

所以,现在你有两个问题:




  1. 您必须inialize _repository 来使用它。你有这样的方式:




    1. 在构造函数中。但如果DbRepository变化的构造是什么?您需要因为代码的另一部分改为重写控制器类。如果你有这不难只有一个控制器,但如果你有一对夫妇有依存类的你有一个真正的问题。

    2. 您可以使用服务定位器或工厂或不便。现在,你有你的服务定位器的依赖。你有一个全球性的服务定位器,所有的代码必须使用它。如何当你需要在一个代码激活逻辑和其他在代码的另一部分的东西的一部分使用你会改变服务定位器的行为?只有一个办法 - 通过服务定位器通过构造函数。但是,随着越来越多的课程,你将需要通过它越来越多。 。无论如何,这是很好的想法,但它是坏的主意。



       类控制器{
      私人DbRepository _repository;

      公共控制器(){
      _repository = GlobalServiceLocator.Get< D​​bRepository>()
      }

      // ...一些方法,使用_repository
      }


    3. 您可以使用依赖注入。看代码:

       类控制器{
      私人IRepository _repository;

      公共控制器(IRepository库){
      _repository =库;
      }
      }




    现在,当你需要你的控制器,你写的: ninjectDevKernel.Get<控制器>(); ninjectTestKernel.Get<控制器>(); 。您可以以最快的速度,只要你想切换beetween依赖解析器。看到?这很简单,你不需要写很多。


  2. 您无法在其上做单元测试。您对 DbRepository 控制器有依赖性,如果你想测试使用存储库的一些方法,你的代码将进入数据库,问它的数据。这是慢,很慢。如果您在 DbRepository 修改代码,在控制器单元测试将下降。只有集成测试必须说,在这种情况下,问题。你需要在单元测试中是什么 - 隔离类和一个测试只测试一个类(在理想的 - 只有一个方法)。如果你的 DbRepository 代码失败,你会觉得控制器代码的失败 - 这是坏的(即使你有做检查 DbRepository 控制器 - 他们都将失败,你可以从错误的地方开始)。这需要大量的时间来确定哪里是真正的错误。你需要知道一些类是确定的,和其他一些类是失败。


  3. 当你想与其他的所有的类的东西代替 DbRepository ,你必须做很多工作的。


  4. 您不能 DbRepository 容易控制寿命。这个类的对象创建的控制器的inializing当控制器删除删除。有没有共享beetween 控制器类的不同实例,也没有共享beetween其他类。随着Ninject可以简单的写:

      kernel.Bind< IRepository>()为< DbRepository>()InSingletonScope() ; 




和依赖注入的特殊功能 - 敏捷开发!你描述你的控制器使用存储库接口 IRepository 。你不需要写 DbRepository ,你可以写简单的 MemoryRepository 类,并制定控制器,而其他球员的发展 DbRepository 。当 DbRepository 将完成,你只要重新绑定在你的依赖解析器的默认 IRepository DbRepository 现在。你已经所著很多控制器?他们都将使用现在 DbRepository 。这很酷。



了解更多:




  1. 控制(维基)反转

  2. 依赖注入(维基)

  3. 控制容器的反转和依赖注入模式(马丁·福勒)


I have been helping a few friends on a project and there is a class that uses Ninject. I am fairly new to C# and I have no idea what that class is doing, which is why I need to understand Ninject. Can anyone explain what Ninject is and when does one use it(with example if possible)? Or if you can point to some links that would be great too.

I tried this question: Ninject tutorials/documentations? but it didn't really help a beginner like me.

解决方案

Ninject is dependency injector for .NET, practical realisation of pattern Dependency Injection (form of Inversion of Control pattern).

Suppose you have two classes DbRepository and Controller:

class Controller {
   private DbRepository _repository;

   // ... some methods that uses _repository
}

class DbRepository {
   // ... some bussiness logic here ...
}

So, now you have two problems:

  1. You must inialize _repository to use it. You have such ways:

    1. In constructor manually. But what if constructor of DbRepository changes? You need to rewrite your Controller class because other part of code was changed. It's not hard if you have only one Controller, but if you have a couple of classes that have dependency on your Repository you have a real problem.
    2. You can use service locator or factory or smth. Now you have dependency on your service locator. You have a global service locator and all code must use it. How you will change behaviour of service locator when you need to use in one part of code one activation logic and something other in other part of code? There is only one way - passing service locator through the constructors. But with more and more classes you will need to pass it more and more. Anyway, that is nice idea, but it is bad idea.

      class Controller {
         private DbRepository _repository;
      
         public Controller() {
           _repository = GlobalServiceLocator.Get<DbRepository>()
         }
      
         // ... some methods that uses _repository
      }
      

    3. You can use dependency injection. Look at the code:

      class Controller {
         private IRepository _repository;
      
         public Controller(IRepository repository) {
            _repository = repository;
         }
      }
      

    Now when you need your controller you write: ninjectDevKernel.Get<Controller>(); or ninjectTestKernel.Get<Controller>();. You can switch beetween dependency resolvers as fast as you want. See? It's simple, you don't need to write a lot.

  2. You can't make unit tests on it. Your Controller have dependency on DbRepository and if you want test some method that uses repository, your code will go to database and ask it for data. That's slow, very slow. If your code in DbRepository changes, your unit test on Controller will fall. Only integration test must say 'problems' in this case. What you need in unit tests - to isolate your classes and test only one class in one test (in ideal - only one method). If your DbRepository code fails, you will think that Controller code failed - and that's bad (even if you have tests for DbRepository and Controller - they both will fail and you can start from wrong place). It takes a lot of time to determine where is error really. You need to know that some class is ok, and some other class was failed.

  3. When you want replace DbRepository with something other in all your classes, you have to do a lot of work.

  4. You can't easy control lifetime of DbRepository. Object of this class creates on inializing of Controller and deletes when Controller deletes. There are no sharing beetween different instances of Controller class and there are no sharing beetween other classes. With Ninject you can simply write:

    kernel.Bind<IRepository>().To<DbRepository>().InSingletonScope();
    

And special feature of dependency injection - agile development! You describe that your controller uses repository with interface IRepository. You don't need to write DbRepository, you can write simple MemoryRepository class and develop Controller while other guys develops DbRepository. When DbRepository will be finished, you just rebinds in your dependency resolver that default IRepository is DbRepository now. You have writed a lot of controllers? All of them will use now DbRepository. That's cool.

Read more:

  1. Inversion of control (wiki)
  2. Dependency injection (wiki)
  3. Inversion of Control Containers and the Dependency Injection pattern (Martin Fowler)

这篇关于什么是Ninject,什么时候使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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