使用实体框架实现 DAL 时增强其性能

发布于 2024-10-09 15:56:31 字数 275 浏览 0 评论 0原文

我正在使用实体框架实现 DAL。我们有一些 DAL 类(我称之为存储库),每次调用方法时都会通过参数实例化或接收上下文。我不喜欢这种行为;我没有关于这个问题的记录,但我的常识告诉我,上下文的实例化消耗了太多的系统资源。那么:

  1. 上下文的实例化成本昂贵吗?
  2. 如果您对 1 的回答是“是”,您将如何从设计角度解决这个问题?
  3. 如果您对 1 的回答是“是”,您将如何在 C# 中实现该解决方案?
  4. 您建议采用哪种方法来为 Web 应用程序实现 DAL?

I'm implementing a DAL using the Entity Framework. We have some DAL classes (I call them repositories) instantiating or receiving a context by parameter every time a method is called. I don't like that kind of behavior; I'm not documented about this issue but my common sense says me that context's instantiation consumes too much system resources. So:

  1. Is context's instantiation expensive?
  2. If you've answered "yes" to 1, how would you tackle this problem from a design viewpoint?
  3. If you've answered "yes" to 1, how would you implement the solution in C#?
  4. Which approach do you recommend to implement a DAL for a web application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我爱人 2024-10-16 15:56:31

我的常识告诉我,上下文的实例化消耗了太多的系统资源

。在优化方面,常识几乎毫无用处。

您认为上下文的构造函数中究竟会出现什么问题?您阅读源代码了吗?

1)上下文的实例化开销大吗?

相对于什么?与建立数据库连接所需的时间相比?与执行站点 DNS 查找所需的时间相比?与浏览器渲染页面所花费的时间相比?

最有可能的是,与通过网络实际检索数据所需的时间相比,上下文的实例化并不是特别耗时。

2) 如果您对 1 的回答是“是”,您将如何从设计角度解决这个问题?

使用 UnitOfWork 抽象每个工作单元< /a> 应该包含单个实体上下文。如果这是一个网络应用程序,则每个请求应该有一个工作单元。

my common sense says me that context's instantiation consumes too much system resources

Common sense is nearly useless when it comes to optimization.

What exactly in the constructor of context do you suppose will be problematic? Have you read the source for it yet?

1) Is context's instantiation expensive?

Relative to what? Compared to the amount of time required to establish a database connection? Compared to the time it takes to perform your site's DNS lookup? Compared to the amount of time a browser might spend rendering your page?

The vast liklihood is that context's instantiation is not particularly time consuming compared to the time required to actually retrieve data across the network.

2) If you've answered "yes" to 1, how would you tackle this problem from a design viewpoint?

Use a UnitOfWork abstraction. Each UnitOfWork should contain a single entity context. If this is a web app you should have one UnitOfWork per request.

橙幽之幻 2024-10-16 15:56:31

使用 ORM 时,上下文生命周期管理至关重要。实体框架上下文保留有关已加载实体的信息以用于延迟加载和更改跟踪目的,并且其内存占用量可能会快速增长。如果你不处理你的上下文,你基本上就会发生内存泄漏。

但是,您是正确的,将上下文生命周期保持得太短并不理想,因为您可能希望利用更改跟踪。

using (var context = new DataContext())
{
    context.Products.Add(product);
    context.SaveChanges();
}

上面的示例显示,处理上下文的速度太快,无法利用更改跟踪功能。

对于 Web 应用程序,您应该为每个请求使用上下文。

对于 Win Form 应用程序,当您从一种窗体移动到另一种窗体时,您可以处理上下文。

Context lifetime management is a crucial when using ORMs. The entity framework context in keeps information about loaded entities for lazy loading and change tracking purposes and its memory footprint may grow very quickly. If you don't dispose your context you will essentially have a memory leak.

However, you are correct that keeping the context lifetime too short is not ideal since you would probably like to make use of change tracking.

using (var context = new DataContext())
{
    context.Products.Add(product);
    context.SaveChanges();
}

The above example shows disposes the context too quickly to take advantage of the change tracking goodies.

For Web applications you should use context per request.

For Win Form applications you can dispose of your context when you move from one form to another.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文