这些实体框架建议是否相互冲突?

发布于 2024-12-16 15:06:03 字数 1961 浏览 1 评论 0 原文

当我一起查看所有这些文章时,我对如何进行 EF 开发有点困惑,因为我找不到在一个地方解决所有这些实践的示例:

问题

  1. 我应该调用 Dispose 吗?我应该在哪里调用它以确保上下文的正确生命周期? ...在 MVC 应用程序中,这似乎是通过重写控制器的 dispose 方法来完成的。

  2. 我应该如何处置上面链接的 Azure 缓存? ...也许 ObjectCache 是我应该关心的唯一对象。

  3. 我应该使用Using,还是使用不可信?

  4. Microsoft 是否应该制作一个解决所有这些问题的示例?该样本会是什么样子? (如果不是 这个)我看到的大多数使用 EF + MVC 的示例都有不同且不一致的实现。我不确定在我的项目中要模仿谁。

I'm a little confused on how to approach EF development when looking at all these articles together, since I can't find a sample that addresses all of these practices in one place:

Question

  1. Should I call Dispose? Where should I call it to ensure proper lifetime of the context? ... In an MVC app this seems to be done by overriding the controller's dispose method.

  2. How should I dispose of the Azure cache linked above? ... Perhaps ObjectCache is the only object I should be concerned about.

  3. Should I use Using, or is using untrustworthy?

  4. Should Microsoft produce a sample that addresses all these issues? What would that sample look like? (if it's not this one) Most samples I see with EF + MVC have a varying and inconsistent implementation. I'm not sure who to imitate in my project.

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

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

发布评论

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

评论(3

草莓酥 2024-12-23 15:06:03

你得出了不必要的结论。

WCF 问题是一个设计缺陷。微软搞砸了。有时会发生这种情况。

我已经没有参考文献了,但是我通过搜索找到了它,你也可以。在 WCF 的设计过程中,有一个问题出现了:“Dispose() 应该始终调用 Close()”。当这个问题问到 Don Box(WCF 的首席架构师或类似头衔)时,他“想不出任何不这样做的理由”。他错过了一个不这样做的理由。

Dispose() 不得抛出异常。这是因为:

try
{
    var proxy = null;
    try
    {
        proxy = new ProxyClass();
        throw new Exception1();
    }
    finally
    {
        if (proxy != null) proxy.Dispose(); // What happens if this throws Exception2?
    }
}
catch (Exception ex)
{
    // Which exception do I see in here?
}

如果 Dispose() 抛出 Exception2,那么我将丢失 Exception1 以及显示发生情况的堆栈跟踪。问题是 Box 先生没有发现为什么 Dispose() 不应该只调用 Close() 来完成这项工作。问题是,对于某些绑定,Close() 实际上必须做一些工作。 wsHttpBinding 就是这种情况,其中在 Close() 上进行消息交换。这意味着 Close() 很有可能抛出异常,从而破坏我的调用堆栈。

You have drawn conclusions unnecessarily.

The WCF issue is a desgn flaw. Microsoft screwed up. It happens sometimes.

I no longer have the reference, but I found it by searching, and you can too. There was a point during the design of WCF where the question came up, "should Dispose() just always call Close()". When the question came to Don Box (the Chief Architect of WCF or some such title), he "couldn't think of any reason why not". He missed a reason why not.

Dispose() must not throw exceptions. This is because of the following:

try
{
    var proxy = null;
    try
    {
        proxy = new ProxyClass();
        throw new Exception1();
    }
    finally
    {
        if (proxy != null) proxy.Dispose(); // What happens if this throws Exception2?
    }
}
catch (Exception ex)
{
    // Which exception do I see in here?
}

If Dispose() throws Exception2, then I will lose Exception1 along with the stack trace showing me what happened. The problem is that Mr. Box found no reason why Dispose() shouldn't just call Close() to do the job. The problem is that, with some bindings, Close() actually has to do some work. This is the case with wsHttpBinding, where there is a message exchange upon Close(). This means ther's a real chance of Close() throwing an exception, trashing my call stack.

此岸叶落 2024-12-23 15:06:03

开始检查你的事实——它们是错误的。关于使用不可靠的文章并没有说它不可靠。处于错误状态的 WCF 对象无法被处置 - 它们已经被自行处置。这就是另一个错误发生的原因。这并不意味着使用不调用处置。鉴于这个事实是错误的,我什至不会对你的结论发表评论——因为显然你的事实是错误的。

Start checking your facts - they are wrong. The article about using being unreliable does not say it is unreliable. WCF objects that are in a faulted state can not be disposed - they are already self-disposed. This is why another error occurs. It does not mean using does not call dispose. Given that this fact is wrong, I would not even tart commenting on your conclusions - because obviously your facts are wrong.

无需解释 2024-12-23 15:06:03

基本思想非常简单。如果您使用 IDisposable 实例,则需要调用 Dispose 方法。问题在于这些实例的范围以及何时处置它们。最好在用完后立即处理掉它们。

人们根据他们的需求以各种方式实现存储库模式。所以这些实现可能不适合你。找出最适合你的。

This post says my MVC applications should override Dispose() to get rid of the context

我从未说过[你]应该重写该方法

The basic idea is very simple. If you are using IDisposable instances you need to call the Dispose method. The problem is the scope of those instances and when you are going to dispose them. Better to dispose them as soon as you are done with them.

People implement the repository pattern in various ways depending on their requirements. So those implementations may not work for you. Work out whats best for you.

This post says my MVC applications should override Dispose() to get rid of the context

I never said [you] should over ride the method

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