MVC4中使用Ninject

1.NuGet获取Ninject.dll

.NET技术交流群 199281001 .欢迎加入。

2.全局注册  Global.asax.cs

 RegisterNinject

//注册Ninject依赖注入全局解析器
2 GlobalConfiguration.Configuration.DependencyResolver = new System.Web.Http.Dependencies.NinjectDependencyResolver(new Ninject.StandardKernel());

3.辅助类

 NinjectDependencyResolver

1 using BLL;
2 using IBLL;
3 using Ninject;
4 using System.Web.Http.Dependencies;
5
6 namespace System.Web.Http.Dependencies
7 {
8 //Author:GaoBingBing
9 public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
10 {
11 [Ninject.Inject]
12 private IKernel kernel;
13 public NinjectDependencyResolver()
14 {
15
16 }
17 public NinjectDependencyResolver(IKernel kernel)
18 {
19 this.kernel = kernel;
20 this.kernel.Settings.InjectNonPublic = true;
21 this.AddBinds();
22 }
23
24 private void AddBinds()
25 {
26
27 //由此添加你的注入
28 this.kernel.Bind<IXX>().To<XX>();
29 }
30 //开始处理
31 public IDependencyScope BeginScope()
32 {
33 return new NinjectDependencyScope(this.kernel.BeginBlock());
34 }
35
36
37
38 }
39 }
 NinjectDependencyScope

using Ninject.Activation;
using Ninject.Parameters;
using Ninject.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;

namespace System.Web.Http.Dependencies
{
//Author:GaoBingBing
public class NinjectDependencyScope : IDependencyScope
{
protected IResolutionRoot resolutionRoot;
public NinjectDependencyScope()
{

}
public NinjectDependencyScope(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public object GetService(Type serviceType)
{
return resolutionRoot.Resolve(this.CreateRequest(serviceType)).SingleOrDefault();
}

public IEnumerable<object> GetServices(Type serviceType)
{
return this.resolutionRoot.Resolve(this.CreateRequest(serviceType));
}
private IRequest CreateRequest(Type serviceType)
{
return resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
}
public void Dispose()
{
this.resolutionRoot = null;
}
}
}

NinjectDependencyScope

4.Config

 DIConfig

//Author:GaoBingBing
public class DIConfig
{
public static T CreateInstance<T>() where T : class
{
System.Web.Http.Dependencies.IDependencyResolver resolver = System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver;
return resolver.BeginScope().GetService(typeof(T)) as T;
}

}

DIConfig

5.调用

private IXX   _x=DIConfig.CreateInstance<IXX>();

6.谢谢关注

 
 
分类: ASP.NETASP.NET MVC 4C#IOC

最新文章

  1. Ajax在静态页面中向指定url发送json请求获取返回的json数据
  2. Powershell学习之道-文件夹共享及磁盘映射
  3. Apache运行模式
  4. poj 2240 Arbitrage (最短路 bellman_ford)
  5. 【P3O是什么】P3O认证之项目组合、项目群和项目办公室
  6. ansible 学习与实践
  7. 关于cocos2d安装时编译不成功(个人心得)
  8. smarty 截取字符串,调用php中的方法,foreach循环
  9. 学SpringMVC收藏
  10. 32.APP后端处理表情的一些技巧
  11. Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听
  12. java-null简介
  13. L258 技术转让
  14. 缩点tarjan
  15. iOS 生成pem证书
  16. POJ 3533 Light Switching Game(三维Nim积)题解
  17. AngularJS自定义Directive
  18. SparkSQL执行时参数优化
  19. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
  20. tornado 04 模板

热门文章

  1. hive建表没使用LZO存储格式,可是数据是LZO格式时遇到的问题
  2. CSDN博客ByeBye
  3. 在ASP.NET 5应用程序中的跨域请求功能详解
  4. ASP.NET MVC5 插件机制中插件的简单实现
  5. HTTP必知必会(转)
  6. Putty是一个专业的SSH连接客户端
  7. 处理器(CPU)调度问题
  8. linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器
  9. auto tool: make -2014-1210-0001
  10. No bean named &amp;#39;sessionFactory&amp;#39; is defined