什么是Ninject,什么时候使用它? [英] What is Ninject and when do you use it?
问题描述
我一直在一个项目帮助几个朋友,并有一个使用Ninject类。我是相当新的C#和我不知道那是什么类是干什么的,这就是为什么我需要了解Ninject。谁能解释Ninject是什么,什么时候做一件使用它(例如带如果可能的话)? 。或者,如果你可以点一些链接,将是巨大的。
我想这个问题:的 Ninject教程/单证?但它并没有真正帮助像我这样的初学者。
Ninject是.NET依赖注入,模式依赖注入的具体实现。<(控制模式的反转形式)/ p>
假设你有两个类 DbRepository
和控制器
:
类控制器{
私人DbRepository _repository;
// ...使用_repository
} $的一些方法B
$ B类DbRepository {
// ...这里的一些经营业务逻辑...
}
所以,现在你有两个问题:
-
您必须inialize
_repository
来使用它。你有这样的方式:
- 在构造函数中。但如果DbRepository变化的构造是什么?您需要因为代码的另一部分改为重写
控制器
类。如果你有这不难只有一个控制器
,但如果你有一对夫妇有依存类的库
你有一个真正的问题。 -
您可以使用服务定位器或工厂或不便。现在,你有你的服务定位器的依赖。你有一个全球性的服务定位器,所有的代码必须使用它。如何当你需要在一个代码激活逻辑和其他在代码的另一部分的东西的一部分使用你会改变服务定位器的行为?只有一个办法 - 通过服务定位器通过构造函数。但是,随着越来越多的课程,你将需要通过它越来越多。 。无论如何,这是很好的想法,但它是坏的主意。
类控制器{
私人DbRepository _repository;
公共控制器(){
_repository = GlobalServiceLocator.Get< DbRepository>()
}
// ...一些方法,使用_repository
}
-
您可以使用依赖注入。看代码:
类控制器{
私人IRepository _repository;
公共控制器(IRepository库){
_repository =库;
}
}
现在,当你需要你的控制器,你写的:
ninjectDevKernel.Get<控制器>();
或ninjectTestKernel.Get<控制器>();
。您可以以最快的速度,只要你想切换beetween依赖解析器。看到?这很简单,你不需要写很多。 - 在构造函数中。但如果DbRepository变化的构造是什么?您需要因为代码的另一部分改为重写
-
您无法在其上做单元测试。您对
DbRepository
控制器
有依赖性,如果你想测试使用存储库的一些方法,你的代码将进入数据库,问它的数据。这是慢,很慢。如果您在DbRepository
修改代码,在控制器单元测试
将下降。只有集成测试必须说,在这种情况下,问题。你需要在单元测试中是什么 - 隔离类和一个测试只测试一个类(在理想的 - 只有一个方法)。如果你的DbRepository
代码失败,你会觉得控制器
代码的失败 - 这是坏的(即使你有做检查DbRepository
和控制器
- 他们都将失败,你可以从错误的地方开始)。这需要大量的时间来确定哪里是真正的错误。你需要知道一些类是确定的,和其他一些类是失败。 -
当你想与其他的所有的类的东西代替
DbRepository
,你必须做很多工作的。 -
您不能
DbRepository
容易控制寿命。这个类的对象创建的控制器
的inializing当控制器
删除删除。有没有共享beetween控制器
类的不同实例,也没有共享beetween其他类。随着Ninject可以简单的写:kernel.Bind< IRepository>()为< DbRepository>()InSingletonScope() ;
和依赖注入的特殊功能 - 敏捷开发!你描述你的控制器使用存储库接口 IRepository
。你不需要写 DbRepository
,你可以写简单的 MemoryRepository
类,并制定控制器
,而其他球员的发展 DbRepository
。当 DbRepository
将完成,你只要重新绑定在你的依赖解析器的默认 IRepository
是 DbRepository
现在。你已经所著很多控制器?他们都将使用现在 DbRepository
。这很酷。
了解更多:
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:
You must inialize
_repository
to use it. You have such ways:- 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 oneController
, but if you have a couple of classes that have dependency on yourRepository
you have a real problem. 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 }
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>();
orninjectTestKernel.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.- In constructor manually. But what if constructor of DbRepository changes? You need to rewrite your
You can't make unit tests on it. Your
Controller
have dependency onDbRepository
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 inDbRepository
changes, your unit test onController
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 yourDbRepository
code fails, you will think thatController
code failed - and that's bad (even if you have tests forDbRepository
andController
- 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.When you want replace
DbRepository
with something other in all your classes, you have to do a lot of work.You can't easy control lifetime of
DbRepository
. Object of this class creates on inializing ofController
and deletes whenController
deletes. There are no sharing beetween different instances ofController
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:
- Inversion of control (wiki)
- Dependency injection (wiki)
- Inversion of Control Containers and the Dependency Injection pattern (Martin Fowler)
这篇关于什么是Ninject,什么时候使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!