MVC三个IOC注入点之Ninject使用示例
2015-07-02 16:32
281 查看
群里一个技术大牛说MVC有三个注入点,但我只会一个DefaultControllerFactory。 在群友的帮助下,我大致了解了下:
IControllerFactory=>IDependencyResolver=>IControllerActivator
这三者的关系如下:
![](http://images.cnitblog.com/blog/142243/201308/26114713-72da5eae97604c6ba9d403a344e35d7b.jpg)
其实从上面的关系可以看出来这三个注入点,相互都存在依赖关系。 我们还是老规矩上代码:
1.IControllerFactory 注入:
2.IControllerActivator 注入:
3.IDependencyResolver注入:
三个在Global.asax的绑定到全局代码如下:
前端控制器实现方式统一如下:
利用构造函数从容器中取出来对应的服务,好了,非常感谢群里的的技术指导。非常感谢
from: http://www.cnblogs.com/flyfish2012/p/3282261.html
IControllerFactory=>IDependencyResolver=>IControllerActivator
这三者的关系如下:
![](http://images.cnitblog.com/blog/142243/201308/26114713-72da5eae97604c6ba9d403a344e35d7b.jpg)
其实从上面的关系可以看出来这三个注入点,相互都存在依赖关系。 我们还是老规矩上代码:
1.IControllerFactory 注入:
public class NInjectFactory:DefaultControllerFactory { private IKernel _iKernel; public NInjectFactory(IKernel ikernel) { this._iKernel = ikernel; AddBindHelper(); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return controllerType == null ? null : (IController)_iKernel.Get(controllerType); } public void AddBindHelper() { _iKernel.Bind(typeof(IProduct)).To(typeof(ProductService)); } }
2.IControllerActivator 注入:
public class NinjectTwoControlActivator : IControllerActivator { private IKernel _ikernel; public NinjectTwoControlActivator(IKernel ikernel) { this._ikernel = ikernel; AddBindHelper(); } public IController Create(RequestContext requestContext, Type controllerType) { return controllerType == null ? null : (IController)_ikernel.Get(controllerType); } public void AddBindHelper() { _ikernel.Bind(typeof(IProduct)).To(typeof(ProductService)); } }
3.IDependencyResolver注入:
public class NinjectThreeCotrolResolver:IDependencyResolver { private IKernel _ikernel; public NinjectThreeCotrolResolver(IKernel ikernel) { this._ikernel = ikernel; AddBindHelper(); } #region IDependencyResolver Members public object GetService(Type serviceType) { try { return _ikernel.Get(serviceType); } catch { return null; } } public IEnumerable<object> GetServices(Type serviceType) { return Enumerable.Empty<object>(); } #endregion public void AddBindHelper() { _ikernel.Bind(typeof(IProduct)).To(typeof(ProductService)); } }
三个在Global.asax的绑定到全局代码如下:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); //ControllerBuilder.Current.SetControllerFactory(new NInjectFactory(new Ninject.StandardKernel()));//注册Ninject Ioc //var factory = new DefaultControllerFactory(new NinjectTwoControlActivator(new Ninject.StandardKernel())); //ControllerBuilder.Current.SetControllerFactory(factory); var dependencyResolver = new NinjectThreeCotrolResolver(new Ninject.StandardKernel()); DependencyResolver.SetResolver(dependencyResolver); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
前端控制器实现方式统一如下:
public class HomeController : Controller { private IProduct _iproduct; public HomeController(IProduct iproduct) { this._iproduct = iproduct; } public ActionResult Index() { var list = new List<Product>() { new Product{ProductName="iphone4s",ProductPrice=3700}, new Product{ProductName="iphon5",ProductPrice=3400} }; ViewBag.Price = _iproduct.GetAll(list); return View(); } }
利用构造函数从容器中取出来对应的服务,好了,非常感谢群里的的技术指导。非常感谢
from: http://www.cnblogs.com/flyfish2012/p/3282261.html
相关文章推荐
- getElementByID,getElementsByName,getElementsByTagName
- 学习使用chromium里的线程类(1)
- iOS开发最常用的第三方库
- poj 2965 The Pilots Brothers' refrigerator 枚举+组合 暑假第二题
- Java项目的*.java 文件打开后注释乱码问题解决办法。
- iOS开发 APP如何实现检测更新(百度)
- awk 筛选特定长度的序列
- 正确调用已知路径DLL的方法
- SSh 免密码登陆,还要求输入密码
- JavaScript & JQuery 交互式Web前端开发
- 一个非常简洁的 jQuery 图片轮播插件
- Objective-C(八、正式协议Protocol,非正式协议informal protocol)——iOS开发基础
- 黑马程序员---集合类
- 在LAMP环境下搭建PHP动态网页
- 理解CSS居中
- 编译frostwire项目jlibtorrent_jni.cpp
- unity3d 切换场景
- 一些iOS常用的第三方库和控件
- Real-Time SQL Monitoring详解
- iOS开发:创建真机调试证书