Ninject依赖注入——构造函数的注入

1、Ninject简介

  Ninject是基于.Net平台的依赖注入框架,它能够将应用程序分离成一个个高内聚、低耦合(loosely-coupled, highly-cohesive)的模块,然后以一种灵活的方式组织起来。Ninject可以使代码变得更容易编写、重用、测试和修改。

  Ninject官方网址为:http://www.ninject.org/

2、项目引用Ninject

1>、 Tools -> Libaary Package Manager -> Package Manager Console,打开Package Manager Console窗口;

2>、在Package Manager Console窗口中输入指令,Enter;

1 PM> Install-Package Ninject

3>、在项目中添加对Ninject的引用。

3、Ninject使用Modules and the Kernel注入

  Ninject中将类别以模块(Module)形式进行分组绑定,每一个模块代表应用程序的一个独立部分,这些模块可以根据需要进行组织。每一个模块都需要实现接口IModule,多数采用扩展StandardModule类来便捷实现。

  Ninject依赖注入包括构造函数、属性、方法和字段的依赖注入。以下先通过构造函数注入方式进行说明,仍采用上节中使用的接口及类。

1>、Ninject构造函数依赖注入(Constructor Injection)

  武器接口IWeapon(IWeapon.cs):

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace NInjectApp
 7 {
 8     /// <summary>
 9   /// 武器
10   /// </summary>
11     public interface IWeapon
12     {
13         void Hit(string target);
14     }
15 }

武器类Sword(Sword.cs):

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace NInjectApp
 7 {
 8     /// <summary>
 9   ///10   /// </summary>
11     public class Sword : IWeapon
12     {
13         public void Hit(string target)
14         {
15             Console.WriteLine("Hit the target {0} by sword", target);
16         }
17     }
18 }

勇士类Samurai(Samurai.cs),在构造函数Samurai中添加[Inject]属性(Attribute)。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Ninject;
 7 
 8 namespace NInjectApp
 9 {
10     /// <summary>
11   /// 勇士
12   /// </summary>
13     public class Samurai
14     {
15         private IWeapon _weapon;
16 
17         [Inject]
18         public Samurai(IWeapon weapon)
19         {
20             _weapon = weapon;
21         }
22 
23         public void Attack(string target)
24         {
25             _weapon.Hit(target);
26         }
27     }
28 }

添加勇士类别绑定模块类WarriorModule(WarriorModule.cs)。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Ninject.Modules;
 7 
 8 namespace NInjectApp
 9 {
10     public class WarriorModule : NinjectModule
11     {
12         public override void Load()
13         {
14             Bind<IWeapon>().To<Sword>();
15             Bind<Samurai>().ToSelf();
16         }
17     }
18 }

创建模块(Module)后,将它们加载到Kernel容器中。调用Kernel的Get()方法,获取Ninject的类别实例。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using Ninject;
 7 
 8 namespace NInjectApp
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             IKernel kernal = new StandardKernel(new WarriorModule());
15             //Samurai s = new Samurai(kernal.Get<IWeapon>()); // 构造函数注入
16             Samurai s = kernal.Get<Samurai>();
17             s.Attack("enemy");
18 
19             Console.ReadKey();
20         }
21     }
22 }

如果需要的话,也可以创建多个模块(Module),将它们参数传递到Kernel的构造函数中。

 1 public class Module1 {
 2       public override  void Load() { ... }
 3 }
 4 
 5 public class Module2 {
 6       public override  void Load() { ... }
 7 }
 8 
 9 class Program {
10       public static void Main()
11       {
12             IKernel kernel = new StandardKernel(new Module1(), new Module2(), ...);
13             ...
14       }
15 }

 

posted @ 2014-05-30 15:35  FlashMeng  阅读(341)  评论(0编辑  收藏  举报