应以一个Web应用程序实体框架的DbContext的范围,我依赖注入如何? (InstancePerHtt prequest VS SingleInstance) [英] How should I scope dependency injection of Entity Framework DbContext in a web app? (InstancePerHttpRequest vs SingleInstance)

查看:326
本文介绍了应以一个Web应用程序实体框架的DbContext的范围,我依赖注入如何? (InstancePerHtt prequest VS SingleInstance)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的DbContext对象应为InstancePerHtt prequest,不SingleInstance,因为它的线程安全性的创造,它可能会消耗requets这让SENCE之间太多的资源。
但我用它使用的DbContext实例存储的对象。我应该让他们InstancePerHtt prequest或使它们SingleInstance并使用DependencyResolver来获取当前的DbContext。

I have read that DbContext object should be created as InstancePerHttpRequest, not SingleInstance, because of its thread-unsafe nature and it might consume too much resource between requets which makes sence. But I am using Repository objects which uses DbContext instance. Should I make them InstancePerHttpRequest or make them SingleInstance and use DependencyResolver to get the current DbContext.

将最好的对象创建的设计是什么,对于Autofac(或任何其他DI)的DbContext,库和基于服务的Web应用程序?

What would the best object creation design be, for Autofac (or any other DI), DbContext, Repository and Service based Web application?

另外一个问题,这是多么昂贵,为每一个存储库或服务,为每个Web请求(如其中10-15的请求)不同的DbContext对象?

Another question, how expensive it is to create a different DbContext object for each repository or service for each web request (like 10-15 of them in a request)?

推荐答案

的DbContext是令人难以置信的便宜实例,所以我不会太担心各地采取以获得一个新的时间。

DbContext is incredibly cheap to instantiate so I wouldn't worry too much around the time taken to get a new one.

我周围的DbContext范围界定的问题是没有这么多线程安全的查询隔离。因为任何人都可以调用保存更改并提交整个对象图你想确保数据库只把它与您的具体变化的环境。

The issue I have with scoping around DbContext isn't so much thread safety its query isolation. Because anyone can call save changes and commit the whole object graph to the database you want to make sure you only call it on a context with your specific changes.

要了解有关的DbContext的另一个关键问题是,性能会下降更多的项目被跟踪。这意味着如果你在单身绑定它,你可能会导致一些pretty严重的性能问题。 (有解决这个,但其真正的好方法要注意看这个 <我的一个帖子href=\"http://blog.staticvoid.co.nz/2012/05/entityframework-performance-and.html\">here)

Another key thing to understand about DbContext is that performance degrades the more items it is tracking. This means if you bind it in singleton you can cause some pretty serious performance issues. (there are ways around this but its really good to be aware of see my post on this here)

有关Web应用程序我个人的preference是在Htt的prequest的范围既上下文和存储库绑定。这意味着,只有你的当前请求的线程将能够保存更改,而且也限制了你很可能会跟踪项目金额。

My personal preference for web apps is to bind both your context and repository in the scope of an HttpRequest. This means that only your current request thread will be able to save changes, and it also limits the amount of items you are likely to track.

不好意思有点我的术语大概不匹配autofac因为我是一个男人ninject自己:)

Sorry a bit of my terminology probably doesn't match autofac as im a ninject man myself :)

这篇关于应以一个Web应用程序实体框架的DbContext的范围,我依赖注入如何? (InstancePerHtt prequest VS SingleInstance)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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