简体   繁体   English

ASP.NET MVC按请求注入

[英]ASP.NET MVC inject per request

I need to inject EF context per request. 我需要为每个请求注入EF上下文。 Is there any way to implement it? 有没有办法实现它?

Did you check out this excellent blog on DI with Unity and ASP.NET MVC? 您是否通过Unity和ASP.NET MVC查看了这篇关于DI的优秀博客?

Should get you on the right track. 应该让你走上正确的轨道。

The answer is yes , you can - and the article shows you how. 答案是肯定的 ,你可以 - 而文章告诉你如何。

In short, you create a HttpContextLifetimeManager to handle the "scoping" of your objects. 简而言之,您创建了一个HttpContextLifetimeManager来处理对象的“范围”。 The container "caches" the instance in the HTTP Context. 容器在HTTP上下文中“缓存”实例。

This is needed because the default life time managers provided by Unity don't cover HTTP Context scoping "off the shelf". 这是必需的,因为Unity提供的默认生命周期管理器不包括“现成的”HTTP上下文范围。

Of course, other DI container's (such as StructureMap - which i use), do. 当然,其他DI容器(如StructureMap - 我使用的)也可以。

Here is another (more up to date) article on the same thing, with the "Nerdinner" as the example. 是关于同一件事的另一篇(更新)文章,以“Nerdinner”为例。

The solution proposed in the Unity Discussion list is to create a child container per request, have that child container create the EF context as ContainerControlledLifetime, then have the child container disposed at the end of the request. Unity讨论列表中提出解决方案是为每个请求创建一个子容器,让该子容器创建EF上下文作为ContainerControlledLifetime,然后将子容器放置在请求的末尾。 By doing so you don't have to create a custom LifetimeManager. 通过这样做,您不必创建自定义LifetimeManager。

I'm not very familiar with Unity but the principle would be something like this: 我对Unity不是很熟悉,但原理是这样的:

Application_BeginRequest(...)
{
  var childContainer = _container.CreateChildContainer();
  HttpContext.Items["container"] = childContainer;
  childContainer.RegisterType<ObjectContext, MyContext>
     (new ContainerControlledLifetimeManager());
}

Application_EndRequest(...)
{
  var container = HttpContext.Items["container"] as IUnityContainer
  if(container != null)
    container.Dispose();
}

What do you mean by injecting? 注射是什么意思? Do you mean to apply dependency inversion principle on it? 你的意思是对它应用依赖倒置原则吗? If yes then do you ever envisage yourself swapping out your EF context with some other context which adheres to same contract? 如果是,那么您是否曾设想将自己的EF背景与其他符合相同合同的背景交换掉?

To me you should encapsulate EF context somewhere in framework, so that every request gets EF DataContext. 对我来说,你应该在框架中的某处封装EF上下文,以便每个请求都获得EF DataContext。 Apply DI on your repository. 在您的存储库中应用DI。 Later on your repositories might have different kind of contexts and you can switch repositories with each other. 稍后您的存储库可能具有不同类型的上下文,您可以相互切换存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个请求的ASP.NET Core MVC注入 - ASP.NET Core MVC inject per request 在ASP.NET Core MVC中注入ApplicationUser - Inject ApplicationUser in ASP.NET Core MVC 每个视图的ASP.NET MVC 1 ViewModel? - ASP.NET MVC 1 ViewModel per View? Asp.net MVC5 Application_Start每个请求都发生-有时每个请求多次 - Asp.net MVC5 Application_Start happens every request - sometimes multiple times per request 如何使用Nhibernate在ASP.NET MVC中实现按请求的会话模式 - How to implement session-per-request pattern in asp.net mvc with Nhibernate 从控制器获取Nhibernate ISession(每个请求会话)ASP.Net MVC - Getting Nhibernate ISession from Controller (Session Per Request) ASP.Net MVC ASP.NET MVC生命周期-每个页面请求一次执行一些工作 - ASP.NET MVC Lifecycle - Do Some Work Once Per Page Request 在asp.net mvc 3中管理每个会话的AutoFac生命周期范围和请求 - Managing AutoFac lifetime scopes per session and request in asp.net mvc 3 ASP.net MVC - 每个视图或每个动作一个ViewModel? - ASP.net MVC - One ViewModel per View or per Action? ASP.NET MVC ActionFilterAttribute在模型绑定之前注入值 - ASP.NET MVC ActionFilterAttribute inject value before model binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM