MVC4中使用Ninject
MVC4中使用Ninject
1.NuGet获取Ninject.dll
.NET技术交流群 199281001 .欢迎加入。
2.全局注册 Global.asax.cs
//注册Ninject依赖注入全局解析器
2 GlobalConfiguration.Configuration.DependencyResolver = new System.Web.Http.Dependencies.NinjectDependencyResolver(new Ninject.StandardKernel());
3.辅助类
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 }
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
//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.谢谢关注
最新文章
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
- Powershell学习之道-文件夹共享及磁盘映射
- Apache运行模式
- poj 2240 Arbitrage (最短路 bellman_ford)
- 【P3O是什么】P3O认证之项目组合、项目群和项目办公室
- ansible 学习与实践
- 关于cocos2d安装时编译不成功(个人心得)
- smarty 截取字符串,调用php中的方法,foreach循环
- 学SpringMVC收藏
- 32.APP后端处理表情的一些技巧
- Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听
- java-null简介
- L258 技术转让
- 缩点tarjan
- iOS 生成pem证书
- POJ 3533 Light Switching Game(三维Nim积)题解
- AngularJS自定义Directive
- SparkSQL执行时参数优化
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
- tornado 04 模板
热门文章
- hive建表没使用LZO存储格式,可是数据是LZO格式时遇到的问题
- CSDN博客ByeBye
- 在ASP.NET 5应用程序中的跨域请求功能详解
- ASP.NET MVC5 插件机制中插件的简单实现
- HTTP必知必会(转)
- Putty是一个专业的SSH连接客户端
- 处理器(CPU)调度问题
- linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器
- auto tool: make -2014-1210-0001
- No bean named &;#39;sessionFactory&;#39; is defined