我可以重写处置做出总是调用的SaveChanges实体类? [英] Can I override Dispose to make an entity class that always calls SaveChanges?

查看:115
本文介绍了我可以重写处置做出总是调用的SaveChanges实体类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个相当细一点,我希望答案是这不是开始用热理念 - 这么说,它有一个点,我很感兴趣,不管,如果有人还跟放纵。



示范代码:

 公共部分类MyEntities:ObjectContext的
{
//的想法是,如果对象是在使用块,这总是被调用?
保护覆盖无效的Dispose(BOOL处置)
{
this.SaveChanges();
base.Dispose(处置);
}
}



客户端代码:

 使用(VAR模型=新MyEntities())
{
//做些什么

//没有担心调用model.SaveChanges()
}



我不确定是问题




  1. 时的处置正确的地方要做到这一点,因为我在想定格出于某种原因 - 我总是困惑于C#的破坏。


  2. 在这种情况下的例外是在客户端代码抛出,一般的SaveChanges将被跳过,这很好,但如果这是如何工作的,我认为,它会一直调用它。我应该使用一个空的捕捉尝试

     公共部分类MyEntities:ObjectContext的
    {
    保护覆盖无效的Dispose(BOOL处置)
    {

    {
    this.SaveChanges();
    }
    赶上{}
    base.Dispose(处置);
    }
    }



解决方案

不要这样做。这是一个坏主意。



的处置的目的是的礼貌的处置非托管资源的早,所以其他进程可以使用它。 处置不应该的语义的 - 它不应该改变你的程序的状态,或以某种方式需要。它应该只能做正是它说,它的作用:的处理资源的



你应该做的终结?的绝对不会的。这是更糟。终结可能根本无法运行,终结运行在另一个线程,可以终结即使对象没有被正确的初始化,等被调用。 编写终结是几乎从来没有做正确的事情,如果你写一个终结它应该只配置一个资源不要做任何幻想的终结。你几乎肯定会写,如果你做一个危险的不正确而脆的节目



要忠于这里的正确原则是:如果需要调用的语义原因然后强制用户将在代码中调用。如果他们忘记了这样做,他们会在测试中发现。 让用户决定是否这样做,将通话在finally块或不正确的事情不要作出这样的决定对他们;您可能决定是错误的。


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:

  1. Is Dispose the right place to do this because I was thinking "Finalize" for some reason - I always get confused on C# destruction.

  2. 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);
        }
    }
    

解决方案

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.

这篇关于我可以重写处置做出总是调用的SaveChanges实体类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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