大体上是把官网上的翻译下而已。

http://www.ninject.90iogjkdcrorg/wiki.html

Dependency Injection By Hand

So what’s Ninject all about? First, let’s examine the idea of dependency injection by walking through a simple example. Let’s say you’re writing the next blockbuster game, where noble warriors do battle for great glory. First, we’ll need a weapon suitable for arming our warriors.

所以Ninject到底是什么?首先,让我们来试验一个依赖注入实例。例如你正在写一个惊天地泣鬼神的游戏,忍者大大(鸣人)为了伟大的荣誉而战,我们需要为我们的武士(下忍们)装备武器。

1 class Sword
2 {
3     public void Hit(string target)
4     {
5         Console.WriteLine("Chopped {0} clean in half", target);
6     }
7 }

Then, let’s create a class to represent our warriors themselves. In order to attack its foes, the warrior will need an Attack() method. When this method is called, it should use its Sword to strike its opponent.

然后让我们新建一个类来代表武士。为了能够进攻敌人,武士类需要一个Attack()方法。当方法被调用的时候,方法应该调用Sword类攻击敌人。

 1 class Samurai
 2 {
 3     readonly Sword sword;
 4     public Samurai()
 5     {
 6         this.sword = new Sword();
 7     }
 8
 9     public void Attack(string target)
10     {
11         this.sword.Hit(target);
12     }
13 }

Now, we can create our Samurai and do battle!
现在,我们可以新建一个武士来作战了!

1 class Program
2 {
3     public static void Main()
4     {
5         var warrior = new Samurai();
6         warrior.Attack("the evildoers");
7     }
8 }

As you might imagine, this will print Chopped the evildoers clean in half to the console. This works just fine, but what if we wanted to arm our Samurai with another weapon? Since the Sword is created inside the Samurai class’s constructor, we have to modify the implementation of the class in order to make this change.

你能想象,控制台将会输出”Chopped the evildoers clean in half“。目前看来一切工作良好,但一旦出现了我们要为武士更换另一种武器的情况怎么办呢?由于剑是新建在武士类的构造器中的,因此为了让武士能够变更武器,我们需要修改整改武士类。

When a class is dependent on a concrete dependency, it is said to be tightly coupled to that class. In this example, the Samurai class is tightly coupled to the Sword class. When classes are tightly coupled, they cannot be interchanged without altering their implementation. In order to avoid tightly coupling classes, we can use interfaces to provide a level of indirection. Let’s create an interface to represent a weapon in our game.

当一个类依赖于一个具体的类时,我们就说这两个类是紧密关联的。在这个例子中,武士类与剑类是紧密关联的。一旦类需要修改时,所依赖的类也需要进行修改。为了避免这种紧密关联的情况,我们可以使用接口来做中间件。让我们来新建一个接口来代表一个武器类。

1 interface IWeapon
2 {
3     void Hit(string target);
4 }

Then, our Sword class can implement this interface:
然后我们的剑类可以继承这个接口

1 class Sword : IWeapon
2 {
3     public void Hit(string target)
4     {
5         Console.WriteLine("Chopped {0} clean in half", target);
6     }
7 }

And we can alter our Samurai class:
之后我们就能更改我们的武士类

 1 class Samurai
 2 {
 3     readonly IWeapon weapon;
 4     public Samurai()
 5     {
 6         this.weapon = new Sword();
 7     }
 8
 9     public void Attack(string target)
10     {
11         this.weapon.Hit(target);
12     }
13 }

Now our Samurai can be armed with different weapons. But wait! The Sword is still created inside the constructor of Samurai. Since we still need to alter the implementation of Samurai in order to give our warrior another weapon, Samurai is still tightly coupled to Sword.
现在我们的武士类就可以装备不同的武器了。但是等等!剑依然在武士类的构造函数中新建啊~由于我们依然需要修改武士类中的声明才能变更武器,所以武士类和剑类依然是紧密关联的。

Fortunately, there is an easy solution. Rather than creating the Sword from within the constructor of Samurai, we can expose it as a parameter of the constructor instead.
幸运的是,这种情况有一个简单的解决方式。我们可以为构造函数传递一个参数,来替代之前的构造函数。

 1 class Samurai
 2 {
 3     readonly IWeapon weapon;
 4     public Samurai(IWeapon weapon)
 5     {
 6         this.weapon = weapon;
 7     }
 8
 9     public void Attack(string target)
10     {
11         this.weapon.Hit(target);
12     }
13 }

Then, to arm our warrior, we can inject the Sword via the Samurai ‘s constructor. This is an example of dependency injection (specifically, constructor injection). Let’s create another weapon that our Samurai could use:
之后就可以武装我们的武士了,我们可以通过武士类的构造函数注入剑类。这个依赖注入的例子展示的是构造器注入。现在让我们新建一个武士可以使用的飞镖类:

1 class Shuriken : IWeapon
2 {
3     public void Hit(string target)
4     {
5         Console.WriteLine("Pierced {0}'s armor", target);
6     }
7 }

Now, we can create an army of warriors:
现在,我们可以新建一个装备的武士了:

 1 class Program
 2 {
 3     public static void Main()
 4     {
 5         var warrior1 = new Samurai(new Shuriken());
 6         var warrior2 = new Samurai(new Sword());
 7         warrior1.Attack("the evildoers");
 8         warrior2.Attack("the evildoers");
 9     }
10 }

This results in the following output to be printed to the console:
结果就是输出以下内容:

【Pierced the evildoers armor.

Chopped the evildoers clean in half.】

This is called dependency injection by hand, because each time you want to create a Samurai, you must first create some implementation of IWeapon and then pass it to the constructor of Samurai. Now that we can change the weapon the Samurai uses without having to modify its implementation, the Samurai class could be in a separate assembly from Sword – in fact, we can create new weapons without needing the source code of the Samurai class!

这个例子就是手动依赖注入,因为每次你需要新建一个武士类,你必须首先新建一些IWeapon的继承类,然后把继承类传递给武士类的构造器。现在我们可以为武士类变更武器而不需要修改IWeapon的继承了,武士类可以单独组装剑类了,实际上我们可以新建新的武器而不需要修改武士类的源代码了!

Dependency injection by hand is an effective strategy for small projects, but as your application grows in size and complexity, it becomes more and more cumbersome to wire all of your objects up. What happens when the dependencies have dependencies of their own? What happens when you want to add a (e.g. caching, tracing to a log, auditing etc.) decorator in front of each instance of a given dependency? You can easily end up spending most of your time creating and wiring together objects, when you could be writing code that adds real value to your software. This is where dependency injection libraries / frameworks like Ninject can help.
手动依赖注入对于小型项目是一个有效的方式,但是一旦你的项目越来越大、越来越复杂,手动依赖注入会变成越来越笨重、越来越难以维护。这个时候就需要依赖注入的项目(Ninject)了。

转载于:https://www.cnblogs.com/JacobHuang/p/6127768.html

Ninject学习(一) - Dependency Injection By Hand相关推荐

  1. [.NET] Rough Dependency Injection

    动机 在设计系统架构的时候,在系统里加入Dependency Injection(DI),让系统可以在不改变程序代码的状况下,抽换类别来提高重用性.扩充性.在.NET里可以选择一些的Framework ...

  2. 再谈angularjs DI(Dependency Injection)

    在前面已经介绍了关于angularjs,以及扩展了一些jQuery ui的一些组件为angularjs的directive.在这里应进口007 在上篇留言我们来看看在angularjs中的DI特性. ...

  3. Spring IoC容器和Dependency Injection模式

    轻松学习Spring<一> IoC容器和Dependency Injection模式 最近公司需要,项目中要用到Spring和Ibatis.趁着过年好好学习学习.Ibatis就如同Hibe ...

  4. Dependency injection in ASP.NET Core

    原文 github地址 ASP.NET Core supports the dependency injection (DI) software design pattern, which is a ...

  5. IoC容器和Dependency Injection模式

    注:本文转自http://insights.thoughtworkers.org/injection/,请尊重译者的劳动成果,转载需注明出处. 2017年3月3日 by Martin Fowler L ...

  6. Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler

    原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...

  7. Spring MVC快速教程:依赖注入 Spring MVC Fast Tutorial: Dependency Injection

    What are we going to build? Use singletons of CarManager and BrandManager instead of creating multip ...

  8. SAP Spartacus 中的依赖注入 Dependency Injection 介绍

    先了解 Angular 中的依赖注入 依赖项是指某个类执行其功能所需的服务或对象.依赖项注入(DI)是一种设计模式,在这种设计模式中,类会从外部源请求依赖项而不是让类自己来创建它们. Angular ...

  9. php 依赖注入框架,依赖注入模式(Dependency Injection)

    依赖注入模式(Dependency Injection) 由 学院君 创建于5年前, 最后更新于 10个月前 版本号 #3 18333 views 16 likes 0 collects 1.模式定义 ...

  10. angular示例_Angular Dependency Injection用示例解释

    angular示例 什么是依赖注入? (What is Dependency Injection?) 动机 (Motivation) Dependency Injection is often mor ...

最新文章

  1. kissy core
  2. gitlab安装各种坑
  3. 关于使用Windows Live Writer
  4. python代码在哪里输入-请问一下这个python代码到底是哪里出了问题?
  5. 做事情不要着急,着急只能把事情做的更加糟糕。。。
  6. Nginx URL 重写
  7. #pragma comment和#pragma 预处理指令详解
  8. 学JS的心路历程-Promise(一)
  9. MAC随机修改批处理
  10. python制作点亮图片_MicroPython拼插编程实例:点亮心形8x8点阵
  11. 给缺少Python项目实战经验的人,赶紧收藏!
  12. simpla是基于laravel5的php,一个基于laravel5.1的后台
  13. Golang实践录:简单的代码片段
  14. git切换分支出现head is now at_git寻根——^和~的区别
  15. 使用saminside破解WindowsXP密码
  16. nessus下载后更新插件问题新的解决方法2020
  17. 【汇编】更灵活的定位内存地址方式——and,or命令、SI、DI变址寄存器、直接寻址[idata]、寄存器间接寻址[bx]、寄存器相对寻址[bx+idata]、基址变址寻址、相对基址变址寻址
  18. Atitit r2017 r1 doc list on home ntpc.docx
  19. 入职 3 天“窃取”上万个代码文件,特斯拉起诉前软件工程师!
  20. RxSwift--RxSwift简介

热门文章

  1. SQL Server游标使用快速上手
  2. P3368 【模板】树状数组 2
  3. C++程序设计基础(1)程序的编译和执行
  4. STemWin5.22在STM32F103上的移植步骤
  5. 软件工程项目需求分析
  6. cmd 编码修改。 牛阿。 解决问题
  7. 【1.0】忘记mysql 密码 如何修改之后
  8. Linux Shell编程参考大全
  9. Garmin APP开发之布局
  10. Lucene 的 Scoring 评分机制