团结终身经理&安培; EF数据上下文 - >最佳实践 [英] Unity Lifetime Managers & EF Data Context --> Best Practice
问题描述
所有,
有已经有很多关于团结终身经理的职位,但我还没有发现有人说出一个很好的经验规则,在这种情况下,你应该总是使用X。让我来形容我的申请,我有一个ASP.NET MVC 4 Web应用程序。我有一个包含3个项目,我的核心项目,该项目拥有所有我的EF东西,一个测试项目,以及MVC Web项目Visual Studio解决方案。我使用Unity依赖注入,并具有以下code现在:
//上下文
container.RegisterType< IDatabaseFactory,DatabaseFactory>(
新ContainerControlledLifetimeManager();
container.RegisterType<&的UnitOfWork GT;(
新ContainerControlledLifetimeManager());
不过,我注意到我的背景是不是每一个新的Web请求重建这就是我想我会想(让我知道如果我错了在这样的假设)。我有一个很难分析所有的从下列和网站的信息已经了解了很多人创建一个名为PerHtt prequestLifetimeManager自己的类来处理这个问题。
什么确实是最好的做法在这里?
- 微软的开发者网络了解生命周期管理 - <一个href=\"http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx\">http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx
- MVC DI&安培;统一通过$ C $的CProject生命周期管理器 - <一个href=\"http://www.$c$cproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager\">http://www.$c$cproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager
- ASP.NET MVC提示:通过石咀Varghese表示的博客依赖注入使用Unity应用程序块 - <一个href=\"http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx\">http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx
- MVC,EF - DataContext的单个实例每个Web请求在通过堆栈溢出统一 - <一个href=\"http://stackoverflow.com/questions/5187562/mvc-ef-datacontext-singleton-instance-per-web-request-in-unity\">MVC, EF - DataContext的单个实例每个Web请求在Unity
- 注入相同的DataContext的实例将通过几种通过堆栈溢出统一 - <一个href=\"http://stackoverflow.com/questions/897205/inject-same-datacontext-instance-across-several-types-with-unity\">Inject在几个类型的统一 相同的DataContext实例
是的,你通常希望每次请求的DbContext。
一个PerHtt prequestLifetimeManager或在每次请求创建子容器是该处理的典型方式。
统一的最新版本推出的团结引导程序的ASP.NET MVC 它有一个新的内置一生经理: PerRequestLifetimeManager
您可以阅读更多开发者指南中依赖注入使用Unity 第3章,依赖注入使用Unity 。
All,
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());
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?
- Understanding Lifetime Managers by Microsoft's Developer Network - http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx
- MVC DI & Unity with Lifetime Manager via CodeProject - http://www.codeproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager
- ASP.NET MVC Tip: Dependency Injection with Unity Application Block via Shiju Varghese's Blog - http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx
- MVC, EF - DataContext singleton instance Per-Web-Request in Unity via Stack Overflow - MVC, EF - DataContext singleton instance Per-Web-Request in Unity
- Inject same DataContext instance across several types with Unity via Stack Overflow - Inject same DataContext instance across several types with Unity
Yes, you usually want one DbContext per request.
A PerHttpRequestLifetimeManager or child container created on every request are the typical ways this is handled.
The latest release of Unity introduces the Unity bootstrapper for ASP.NET MVC which has a new built-in lifetime manager: PerRequestLifetimeManager
.
You can read more in the Developer's Guide to Dependency Injection Using Unity chapter 3, Dependency Injection with Unity.
这篇关于团结终身经理&安培; EF数据上下文 - &GT;最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!