我可以重写 Dispose 来创建一个始终调用 SaveChanges 的实体类吗?
这是一个相当好的观点,我希望答案是“这从一开始就不是一个热门的想法”——也就是说,如果有人足够好意的话,它有一些我感兴趣的观点。
模型代码:
public partial class MyEntities : ObjectContext
{
// the idea is if the object is in a using block, this always gets called?
protected override void Dispose(bool disposing)
{
this.SaveChanges();
base.Dispose(disposing);
}
}
客户端代码:
using(var model = new MyEntities())
{
// do something
// no worry about calling model.SaveChanges()
}
我不确定的问题是:
Dispose 是否是执行此操作的正确位置,因为出于某种原因我在考虑“最终化” - 我总是对 C# 销毁感到困惑。
如果客户端代码中抛出异常,通常会跳过 SaveChanges,这很好,但如果这按照我的想法工作,它总是会调用它。我应该将 try 与空 catch 一起使用吗?
公共分部类 MyEntities : ObjectContext { 受保护的覆盖无效处置(布尔处置) { 尝试 { this.SaveChanges(); } 抓住 {} 基础.处置(处置); } }
This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge.
Model Code:
public partial class MyEntities : ObjectContext
{
// the idea is if the object is in a using block, this always gets called?
protected override void Dispose(bool disposing)
{
this.SaveChanges();
base.Dispose(disposing);
}
}
Client Code:
using(var model = new MyEntities())
{
// do something
// no worry about calling model.SaveChanges()
}
The issues I'm uncertain about are:
Is Dispose the right place to do this because I was thinking "Finalize" for some reason - I always get confused on C# destruction.
In the case an exception is thrown in the client code, ordinarily SaveChanges would be skipped and that's good, but if this works how I think, it'll always call it. Should I use try with an empty catch?
public partial class MyEntities : ObjectContext { protected override void Dispose(bool disposing) { try { this.SaveChanges(); } catch {} base.Dispose(disposing); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要这样做。这是一个坏主意。
“Dispose”的目的是尽早礼貌地处置非托管资源,以便其他进程可以使用它。 “Dispose”不应该具有语义——它不应该改变程序的状态或者以某种方式被要求。它应该只做它所说的事情:处置资源。
您应该在终结器中执行此操作吗? 绝对不是。那就更糟了。终结器可能根本不运行,终结器在另一个线程上运行,即使对象未正确初始化也可以调用终结器,等等。 编写终结器几乎从来都不是正确的事情,而且如果您确实编写了终结器,它也应该只处理资源。不要在终结器中做任何花哨的事情;不要在终结器中做任何花哨的事情;如果你这样做,你几乎肯定会编写出一个危险且不正确且脆弱的程序。
这里坚持的正确原则是:如果出于语义原因需要调用,则强制用户将调用放入代码中。如果他们忘记这样做,他们会在测试中找到答案。 让用户决定将调用放入finally 块中是否正确。不要为他们做出这个决定;你可能决定错误。
Don't do this. It's a bad idea.
The purpose of "Dispose" is to politely dispose of an unmanaged resource early so that other processes can use it. "Dispose" should not have semantics -- it should not change the state of your program or be required in some way. It should only do precisely what it says it does: dispose of a resource.
Should you do it in the finalizer? Absolutely not. That's even worse. The finalizer might not run at all, the finalizer runs on another thread, the finalizer can be called even if the object wasn't properly initialized, and so on. Writing a finalizer is almost never the right thing to do, and if you do write a finalizer it should only dispose of a resource. Do not do anything fancy in a finalizer; you will almost certainly write a dangerously incorrect and brittle program if you do.
The right principle to cleave to here is: if a call is required for semantic reasons then force the user to put the call in the code. If they forget to do it, they'll find out in testing. Let the user decide whether it is the right thing to do to put the call in a finally block or not. Don't make that decision for them; you might decide wrong.
Dispose 是您执行此操作的位置(如果您确实要执行此操作)。
这是您不应该不这样做的原因之一。
Dispose is where you would do this, if you were to do it at all.
This is one of the reasons you should not do it.