群里一个技术大牛说MVC有三个注入点,但我只会一个DefaultControllerFactory。 在群友的帮助下,我大致了解了下:

IControllerFactory=>IDependencyResolver=>IControllerActivator

这三者的关系如下:

其实从上面的关系可以看出来这三个注入点,相互都存在依赖关系。 我们还是老规矩上代码:

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

最新文章

  1. Oracle ------ SQLDeveloper中SQL语句格式化快捷键
  2. java和h5 canvas德州扑克开发中(二)
  3. C#分布式缓存Couchbase使用
  4. 每天一个linux命令(14):which命令
  5. 如何在KVM中管理存储池
  6. &lt;译&gt;Selenium Python Bindings 3 - Navigating
  7. hashtable 和dictionary
  8. 登录记住账号和密码小Demo
  9. 让 IE支持圆角的方法
  10. js初级入门
  11. React 中的this.setState
  12. mysql备份与还原!
  13. linus 下redis守护进程启动
  14. 记录linux 命令
  15. shell 键盘输入
  16. python3内置函数大全(顺序排列)
  17. sqlvarchar后自动填充空格解决办法(SET ANSI_PADDING)
  18. Algorithm——Add Two Numbers(补上周)
  19. egg.js-基于koa2的node.js进阶(一)
  20. Java考试题之五

热门文章

  1. [ 转载]JAVA Socket超时浅析
  2. swift基础--字符串
  3. Java第一阶段总结
  4. EL表达式中如何截取字符串
  5. protobuf 向前兼容向后兼容
  6. [JavaScript] js验证身份证
  7. uva 558 Bellman_Ford
  8. FiddlerScript开发
  9. POJ3282+模拟
  10. 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)