IOC框架实践之Ninject

1.   入门

1.1.  面向接口

01 public interface IWeapon
02 {
03     void Attack(string target);
04 }
05 public class Sword : IWeapon
06 {
07     public void Attack(string target)
08     {
09         Console.WriteLine("Attack " + target + " by sword");
10     }
11 }
12 public class Knife : IWeapon
13 {
14     public void Attack(string target)
15     {
16         Console.WriteLine("Attack " + target + " by knife");
17     }
18 }

1.2.  类型绑定

1 public class WeaponModule : NinjectModule
2 {
3     public override void Load()
4     {
5         this.Bind().To();
6     }
7 }

1.3.  简单应用

1 static void Main(string[] args)
2 {
3     IKernel kernel = new StandardKernel(new WeaponModule());
4     IWeapon weapon = kernel.Get();
5     weapon.Attack("小明");
6     Console.ReadKey();
7 }

2.   进阶

2.1.  对象作用域

作用域

绑定方法

说明

Transient

.InTransientScope()

每次调用创建新实例。

Singleton

.InSingletonScope()

单例,仅创建一个实例。

Thread

.InThreadScope()

每一个线程创建一个实例。

Request

.InRequestScope()

每当Web请求发起时创建一个实例,结束请求时释放实例。

 

2.2.  复杂绑定处理

1 this.Bind().To().Named("Sword");
2 this.Bind().To().Named("Knife");
3 IWeapon weapon1 = kernel.Get("Sword");
4 IWeapon weapon2 = kernel.Get("Knife");

其他方法:https://github.com/ninject/ninject/wiki/Contextual-Binding

2.3.  对象创建过程

详见:https://github.com/ninject/ninject/wiki/The-Activation-Process

3.   扩展

3.1.  动态载入模块

1 IKernel kernel = new StandardKernel();
2 kernel.Load(AppDomain.CurrentDomain.GetAssemblies());

注:需要引入CommonServiceLocator.NinjectAdapter.dll

3.2.  扩展Asp.Net MVC

01 // 重写ControllerFactory
02 public class NinjectControllerFactory : DefaultControllerFactory
03 {
04     private IKernel _kernel = new StandardKernel(new PersonModule());
05     protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
06     {
07         if (controllerType == nullreturn null;
08         return (IController)_kernel.Get(controllerType);
09     }
10 }
11  
12 // 重新设置ControllerFactory
13 protected void Application_Start()
14 {
15     AreaRegistration.RegisterAllAreas();
16     RegisterGlobalFilters(GlobalFilters.Filters);
17     RegisterRoutes(RouteTable.Routes);
18     // 设置ControllerFactory为NinjectControllerFactory
19     ControllerBuilder.Current.SetControllerFactory(newNinjectControllerFactory());
20 }

示例下载: NinjectDemo.zip

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值