Unity Lifetime Managers &EF数据上下文-->最佳实践 [英] Unity Lifetime Managers & EF Data Context --> Best Practice

查看:22
本文介绍了Unity Lifetime Managers &EF数据上下文-->最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

有很多关于 Unity Lifetime Managers 的帖子,但我还没有找到有人陈述过在这些情况下你应该总是使用 X"的好的经验法则.让我描述一下我的应用程序,我有一个 ASP.NET MVC 4 Web 应用程序.我有一个包含 3 个项目的 Visual Studio 解决方案,我的核心"项目包含我所有的 EF 内容、一个测试项目和 MVC Web 项目.我正在使用 Unity 进行依赖注入,现在有以下代码:

There has been a lot of posts about Unity Lifetime Managers but I have yet to find someone state a good rule of thumb for "in these cases you should always use X". Let me describe my application, I have an ASP.NET MVC 4 Web Application. I have a Visual Studio solution containing 3 projects, my 'Core' project which has all of my EF stuff, a testing project, and the MVC Web Project. I am using Unity for dependency injection and have the following code right now:

// Context
container.RegisterType<IDatabaseFactory, DatabaseFactory>(
    new ContainerControlledLifetimeManager();
container.RegisterType<UnitOfWork>(
    new ContainerControlledLifetimeManager());

但是,我注意到我的上下文并没有随着每个新的 Web 请求重新创建,这正是我想要的(如果我的假设有误,请告诉我).我很难分析下面列出的网站中的所有信息,并且读到很多人创建自己的名为 PerHttpRequestLifetimeManager 的类来处理这个问题.

However, I'm noticing that my context is not recreated with every new web request which is what I think I would want (let me know if I'm wrong in that assumption). I'm having a hard time analyzing all of the information from the sites listed below and have read about a lot of people creating their own class named PerHttpRequestLifetimeManager to handle this.

这里真正的最佳实践是什么?

What truly is the best practice here?

  1. 通过 Microsoft 的开发人员网络了解 Lifetime Manager - http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx
  2. MVC DI &通过 CodeProject 与 Lifetime Manager 统一 - http://www.codeproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager
  3. ASP.NET MVC 提示:通过 Shiju Varghese 的博客使用 Unity 应用程序块进行依赖注入 - http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx
  4. MVC、EF - DataContext 单例实例 Per-Web-Request 通过 Stack Overflow - MVC, EF - DataContext 单例实例 Per-Web-Request in Unity
  5. 通过堆栈溢出使用 Unity 在多种类型中注入相同的 DataContext 实例 - 使用 Unity 在多种类型中注入相同的 DataContext 实例

推荐答案

是的,您通常需要每个请求一个 DbContext.

Yes, you usually want one DbContext per request.

针对每个请求创建的 PerHttpRequestLifetimeManager 或子容器是典型的处理方式.

A PerHttpRequestLifetimeManager or child container created on every request are the typical ways this is handled.

最新版本的 Unity 引入了 用于 ASP.NET MVC 的 Unity 引导程序,它有一个新的内置生命周期管理器:PerRequestLifetimeManager代码>.

The latest release of Unity introduces the Unity bootstrapper for ASP.NET MVC which has a new built-in lifetime manager: PerRequestLifetimeManager.

您可以在开发人员指南中阅读更多内容使用 Unity 进行依赖注入第 3 章,Unity 依赖注入.

You can read more in the Developer's Guide to Dependency Injection Using Unity chapter 3, Dependency Injection with Unity.

这篇关于Unity Lifetime Managers &amp;EF数据上下文-->最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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