团结终身经理& EF数据上下文 - >最佳实践 [英] Unity Lifetime Managers & EF Data Context --> Best Practice

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

问题描述

全部



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

  //上下文
container.RegisterType& (
新的ContainerControlledLifetimeManager());

但是,我注意到,我的上下文没有重新创建每个新的Web请求,这是我想我想要的(让我知道,如果我错了假设),我很难分析下面列出的所有信息,并且已经阅读了很多人创建自己的名为PerHttpRequestLifetimeManager的类来处理这个。




  1. 了解Microsoft开发人员网络中的终身经理 - htt p://msdn.microsoft.com/en-us/library/ff660872(v = PandP.20).aspx

  2. MVC DI&通过CodeProject与终身经理团结一致 - 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单例实例通过堆栈溢出的Unity中的每个Web请求 -

  5. 注入相同的DataContext几个典型的实例es通过堆栈溢出 - 在多个类型中插入相同的DataContext实例使用Unity


解决方案

是的,你通常需要每个请求一个DbContext。 / p>

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



最新版本用于ASP.NET MVC的Unity引导程序,它具有一个新的内置生命周期管理器: 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?

  1. Understanding Lifetime Managers by Microsoft's Developer Network - http://msdn.microsoft.com/en-us/library/ff660872(v=PandP.20).aspx
  2. MVC DI & Unity with Lifetime Manager via CodeProject - http://www.codeproject.com/Articles/424743/MVC-DI-Unity-with-Lifetime-Manager
  3. 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
  4. MVC, EF - DataContext singleton instance Per-Web-Request in Unity via Stack Overflow - MVC, EF - DataContext singleton instance Per-Web-Request in Unity
  5. 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屋!

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