ASP.NET MVC按请求注入

时间:2022-12-04 18:31:35

I need to inject EF context per request. Is there any way to implement it?

我需要为每个请求注入EF上下文。有没有办法实现它?

3 个解决方案

#1


5  

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. By doing so you don't have to create a custom LifetimeManager.

Unity讨论列表中提出的解决方案是为每个请求创建一个子容器,让该子容器创建EF上下文作为ContainerControlledLifetime,然后将子容器放置在请求的末尾。通过这样做,您不必创建自定义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();
}

#2


6  

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. The container "caches" the instance in the HTTP Context.

简而言之,您创建了一个HttpContextLifetimeManager来处理对象的“范围”。容器在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”为例。

#3


1  

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. Apply DI on your repository. Later on your repositories might have different kind of contexts and you can switch repositories with each other.

对我来说,你应该在框架中的某处封装EF上下文,以便每个请求都获得EF DataContext。在您的存储库中应用DI。稍后您的存储库可能具有不同类型的上下文,您可以相互切换存储库。

#1


5  

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. By doing so you don't have to create a custom LifetimeManager.

Unity讨论列表中提出的解决方案是为每个请求创建一个子容器,让该子容器创建EF上下文作为ContainerControlledLifetime,然后将子容器放置在请求的末尾。通过这样做,您不必创建自定义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();
}

#2


6  

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. The container "caches" the instance in the HTTP Context.

简而言之,您创建了一个HttpContextLifetimeManager来处理对象的“范围”。容器在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”为例。

#3


1  

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. Apply DI on your repository. Later on your repositories might have different kind of contexts and you can switch repositories with each other.

对我来说,你应该在框架中的某处封装EF上下文,以便每个请求都获得EF DataContext。在您的存储库中应用DI。稍后您的存储库可能具有不同类型的上下文,您可以相互切换存储库。