如何存储Ninject内核在ASP.NET应用程序? [英] How to store Ninject Kernel in an ASP.NET Application?

查看:116
本文介绍了如何存储Ninject内核在ASP.NET应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真搞不清楚小部分,一个文件的例子,其中有一个Ninject内核无处不在,而不显示如何真正获得该内核的应用程序。

(1)我们应该实例化一个谷粒并将其保存在静态的背景下?或者我们应该实例在每次请求一个不同的(*中的Application_BeginRequest *)

(2)如果它是每个请求内核,那么为什么初始化()的NinjectWebCommon.cs的方法(会在你安装的NuGet包的)是呼吁的Application_Start因为它调用bootstrapper.Initialize(CreateKernel) - <一href="https://github.com/bbraithwaite/SmsQuiz/blob/master/src/BB.SmsQuiz.Api/App_Start/NinjectWebCommon.cs"相对=nofollow> NinjectWebCommon.cs

(3)如果它是一个全局静态内核,那么,InRequestScope()不工作。它执行下列code,而且因为ApplicationStart()时没有要求返回null。

  kernel.Components.GetAll&LT; INinjectHttpApplicationPlugin&GT;()
  。选择(C =&GT; c.RequestScope)
  .FirstOrDefault(S =&GT;!S = NULL);
 

(4)此外,如果是每个请求内核,我们将在哪里保存内核? HttpContext.Current?又是什么意思使用InRequestScope(),如果我要使用HttpContext.Current反正?

解决方案
  

(1)我们是否应该实例化一个谷粒并将其保存在静态的背景下?   或者我们应该实例化一个不同的每个请求(*在   的Application_BeginRequest *)

One和单内核。

  

(2)如果是每个请求内核,那么为什么初始化()的方法   NinjectWebCommon.cs(得到,当您安装的NuGet包)是   呼吁的Application_Start,因为它要求   bootstrapper.Initialize(CreateKernel) - NinjectWebCommon.cs

这不是每个请求的内核。

  

(3)如果是一个全局静态内核,那么,InRequestScope()   不工作。它执行下列code,并且返回null   因为ApplicationStart()的时间没有要求。

     

kernel.Components.GetAll()。选择(C   => c.RequestScope).FirstOrDefault(S => S = NULL);!

这是完全正常的。你不能指望从你的内核,你明确地与InRequestScope注册的HTTP请求外得到一个实例。

  

(4)此外,如果是每个请求内核,我们将在哪里保存   核心? HttpContext.Current?那么什么是使用的意义   InRequestScope(),如果我要使用HttpContext.Current呢?

无处。不保存内核。您可以使用该内核在你的应用程序的启动配置只有一次的DI容器,然后所有依赖关系自动注入。如果你需要的地方内核的应用程序,比在这里你配置你的依赖的地方等你有严重的设计问题,因为你不再使用依赖注入,但服务定位它是一个反模式。

I'm really confused about small, partial, one-file examples where there's a Ninject kernel everywhere without showing how to really get to that kernel in the application.

(1) Should we instantiate one Kernel and keep it in "static" context? Or should we instantiate a different one on every request (*in Application_BeginRequest*)

(2) If it is "kernel per request", then why Initialize() method of the NinjectWebCommon.cs (gets when you install the NuGet package) is called on Application_Start since it calls bootstrapper.Initialize(CreateKernel) - NinjectWebCommon.cs

(3) If it is "one global static kernel" then the "InRequestScope()" doesn't work. It executes the following code and that returns null because at ApplicationStart() time there's no request.

kernel.Components.GetAll<INinjectHttpApplicationPlugin>()
  .Select(c => c.RequestScope)
  .FirstOrDefault(s => s != null);

(4) Again, if it is "kernel per request", where will we save the kernel? HttpContext.Current? Then what is meaning of using InRequestScope() if I'm going to use HttpContext.Current anyway?

解决方案

(1) Should we instantiate one Kernel and keep it in "static" context? Or should we instantiate a different one on every request (*in Application_BeginRequest*)

One and single kernel.

(2) If it is "kernel per request", then why Initialize() method of the NinjectWebCommon.cs (gets when you install the NuGet package) is called on Application_Start since it calls bootstrapper.Initialize(CreateKernel) - NinjectWebCommon.cs

It isn't kernel per request.

(3) If it is "one global static kernel" then the "InRequestScope()" doesn't work. It executes the following code and that returns null because at ApplicationStart() time there's no request.

kernel.Components.GetAll() .Select(c => c.RequestScope) .FirstOrDefault(s => s != null);

That's perfectly normal. You cannot expect to get an instance from your kernel which you explicitly registered with InRequestScope outside of an HTTP Request.

(4) Again, if it is "kernel per request", where will we save the kernel? HttpContext.Current? Then what is meaning of using InRequestScope() if I'm going to use HttpContext.Current anyway?

Nowhere. You DON'T SAVE THE KERNEL. You use the kernel to configure your DI container only once at your application startup and then all dependencies are automatically injected. If you need the kernel somewhere in your application, other than the place where you configured your dependencies you have a serious design problem because you are no longer using Dependency Injection but Service Location which is an anti-pattern.

这篇关于如何存储Ninject内核在ASP.NET应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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