Ninject学习(一) - Dependency Injection By Hand
大体上是把官网上的翻译下而已。
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到底是什么?首先,让我们来试验一个依赖注入实例。例如你正在写一个惊天地泣鬼神的游戏,忍者大大(鸣人)为了伟大的荣誉而战,我们需要为我们的武士(下忍们)装备武器。
class Sword
{
public void Hit(string target)
{
Console.WriteLine("Chopped {0} clean in half", target);
}
}
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类攻击敌人。
class Samurai
{
readonly Sword sword;
public Samurai()
{
this.sword = new Sword();
} public void Attack(string target)
{
this.sword.Hit(target);
}
}
Now, we can create our Samurai and do battle!
现在,我们可以新建一个武士来作战了!
class Program
{
public static void Main()
{
var warrior = new Samurai();
warrior.Attack("the evildoers");
}
}
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.
当一个类依赖于一个具体的类时,我们就说这两个类是紧密关联的。在这个例子中,武士类与剑类是紧密关联的。一旦类需要修改时,所依赖的类也需要进行修改。为了避免这种紧密关联的情况,我们可以使用接口来做中间件。让我们来新建一个接口来代表一个武器类。
interface IWeapon
{
void Hit(string target);
}
Then, our Sword class can implement this interface:
然后我们的剑类可以继承这个接口
class Sword : IWeapon
{
public void Hit(string target)
{
Console.WriteLine("Chopped {0} clean in half", target);
}
}
And we can alter our Samurai class:
之后我们就能更改我们的武士类
class Samurai
{
readonly IWeapon weapon;
public Samurai()
{
this.weapon = new Sword();
} public void Attack(string target)
{
this.weapon.Hit(target);
}
}
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.
幸运的是,这种情况有一个简单的解决方式。我们可以为构造函数传递一个参数,来替代之前的构造函数。
class Samurai
{
readonly IWeapon weapon;
public Samurai(IWeapon weapon)
{
this.weapon = weapon;
} public void Attack(string target)
{
this.weapon.Hit(target);
}
}
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:
之后就可以武装我们的武士了,我们可以通过武士类的构造函数注入剑类。这个依赖注入的例子展示的是构造器注入。现在让我们新建一个武士可以使用的飞镖类:
class Shuriken : IWeapon
{
public void Hit(string target)
{
Console.WriteLine("Pierced {0}'s armor", target);
}
}
Now, we can create an army of warriors:
现在,我们可以新建一个装备的武士了:
class Program
{
public static void Main()
{
var warrior1 = new Samurai(new Shuriken());
var warrior2 = new Samurai(new Sword());
warrior1.Attack("the evildoers");
warrior2.Attack("the evildoers");
}
}
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)了。
Ninject学习(一) - Dependency Injection By Hand的更多相关文章
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- 【译】Dependency Injection with Autofac
先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...
- Ninject学习笔记<二>
本文转载自kuangkro 如果给您带来不便请联系博主 一.控制反转和依赖注入 Ninject是一个轻量级的基于.Net平台的依赖注入(IOC)框架.所谓的IOC,即控制反转(Inversion of ...
- Ninject学习笔记<一>
本文转载自永远的阿哲 如果给您带来不便请联系博主. Ninject是一款.Net平台下的开源依赖注入框架.按照官方说法,它快如闪电.超级轻量,且充分利用了.Net的最新语法,使用Lambda表达式代替 ...
- Json.NET Updates: Merge, Dependency Injection, F# and JSONPath Support
Json.NET 6.0 received 4 releases this year, the latest last week. Over these releases, several new f ...
- Dependency Injection in ASP.NET Web API 2 Using Unity
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- [LINK]List of .NET Dependency Injection Containers (IOC)
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx I'm trying to expand my ...
随机推荐
- commons configuration管理项目的配置文件
Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...
- 使用bat(批处理文件类型)两步更改笔记本IP
一.背景 在南农工的第三年里,学校终于给教学区覆盖了无线网NJAUPK,这解决了我在汇贤楼教室上自习没网写web的尴尬处境!经常在9栋和汇贤楼教学区之间来回,遇见了一个大问题:宿舍里无线需要更改IPV ...
- 学习Struts2的第一个应用步骤
---恢复内容开始--- 一.在官网 http://struts.apache.org/下载struts-2.3.31-all 1. 打开struts-2.3.31-all,将struts-2.3. ...
- 关于我-dinphy简介
别 名:孜_行 英文名:dinphy QQ交流群:588266650 兴趣爱好:听音乐.打篮球.热衷于诗词文学 专 业:计算机 了 解:windows及Linux.android的基本 ...
- c# json总结
json确实很好用,但是网上写的很多都很复杂,不适合自己,然后每次写了又记不住,又要反复找,所以将其中的代码写下来.以后看这个就可以了 都引用了 Newtonsoft.Json 1.ashx,asmx ...
- javascript随机打乱数组
var arr=[]; ;i<;i++){ arr[i]=i; } arr.sort(function(){ return 0.5 - Math.random() }) var str=arr. ...
- python模块(shelve,xml,configparser,hashlib,logging)
1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...
- 关于win7 安装redis的问题
首先在https://github.com/MSOpenTech/redis/releases下载64位的安装包 到任意盘中 将改名为redis 使用cmd命令 启动redis 进入 redis 目录 ...
- js loaclstorage和sessionstorage
这里需要注意的是这两种储存方式只能以字符串的形式来存取 html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于 ...
- SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed
环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...