Ninject.MVC3引导程序的内核属性标记为已过时。我怎样才能访问内核? [英] Ninject.MVC3 Bootstrapper's Kernel property is marked as Obsolete. How can I get access to the kernel?

查看:266
本文介绍了Ninject.MVC3引导程序的内核属性标记为已过时。我怎样才能访问内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Ninject.MVC3更新包从2.2.1.0到2.2.2.0。之前,我曾获得过BootStrapper.Kernel财产,但在新版本内核属性的内核对象被标记为过时。我得到一个警告说

I updated Ninject.MVC3 package from 2.2.1.0 to 2.2.2.0. Before I had access to the Kernel object through BootStrapper.Kernel property but in the new version Kernel property is marked as obsolete. I get a warning saying

公共只读属性内核Ninject.IKernel已过时:'不要使用Ninject的服务定位器。

'Public ReadOnly Property Kernel As Ninject.IKernel' is obsolete: 'Do not use Ninject as Service Locator'.

是否有不同的方式来访问新版本的内核?

Is there a different way to access the kernel in the new version?

推荐答案

为什么这个已被标记过时,未来将改变为内部原因是,人们倾向于使用Ninject作为服务定位器,如果它是可以做到所以。但服务定位是不应该使用反模式。当我们不想提供帮助建筑设计糟糕的软件将在未来被删除的功能。

The reason why this has been marked Obsolete and will be changed to internal in future is that people tend to use Ninject as a service locator if it is possible to do so. But Service Locator is an antipattern that should not be used. And as we don't want to provide functionality that helps building badly designed software it will be removed in future.

如果这需要很多的变化在code这是签署您的code从此萎靡不振依赖注入痛苦明智和你真的应该将其更改为更好的设计。

If this needs a lot of changes in your code this is sign that your code is suffering from this malaise Dependency Injection wise and you really should change it to a better design.


  1. 限制内核到mimimum您的访问。有一个在MVC几乎没有任何情况下,你需要比普通的构造函数注入以外的东西。因此,我的第一个建议是重构构造函数注入在可能的情况。

  2. 对于这些,你需要内核访问创建其他对象,你应该注入一个工厂需要新的实例类,注入内核到这个工厂(非常罕见的情况下,如果构造函数有一个内核参数,它会收到实例做注射)。

  1. Limit your access to the kernel to a mimimum. There is almost no situation in MVC where you need something other than plain constructor injection. Therefore my first advice is to refactor to constructor injection where possible.
  2. For these very rare cases where you need access to the kernel to create other objects you should inject a factory to the class that needs the new instance and inject the kernel into this factory (if the Constructor has a Kernel parameter, it'll receive the instance doing the injecting).

如果你真的想留在服务定位器即使几乎所有人都会告诉你不是,你将不得不自己保持静态引用。

If you really want to stay with service locator even if almost everyone will tell you not to, you will have to keep a static reference yourself.

这篇关于Ninject.MVC3引导程序的内核属性标记为已过时。我怎样才能访问内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆