Should IDisposable be applied cascadingly?

前端 未结 9 1539
灰色年华
灰色年华 2020-12-31 06:48

This is a rather basic question, however I\'m still struggling with it a little.

IDisposable is implemented, when you want to enable the user of an object to free un

9条回答
  •  礼貌的吻别
    2020-12-31 07:01

    There are two distinct scenarios:

    1. Your object is given an object reference to use, either through a constructor argument or a property, and this object implements IDisposable.
    2. Your object constructs an instance of an object that implements IDisposable.

    In the second case, your object is responsible for the resources involved, so your object must implement IDisposable, and when disposed of, you should dispose of the object you constructed.

    Your DbConnection falls under this second case, so yes, your object should implement IDisposable, and then dispose of the connection.

    In the first case, you need to decide on the following three solutions:

    1. Your object is only referencing an external object. Your object should not dispose of this external object. You do not need to implement IDisposable for this case (that is, for this specific object, if you also internally constructs a disposable object, you're back to the second case above).
    2. Your object is taking responsibility for the external object. In this case, you're back to the second case, even though your object was not the one constructing this external object. Here you implement IDisposable, and dispose of the object you're given.
    3. You implement a way for the outside world to tell you which of the first two solutions to pick. For instance, a constructor might be given a connection, and a boolean argument (or ideally an enum value) that tells the constructor whether your object now owns the supplied connection. Here you also need to implement IDisposable, but in the Dispose method, you need to check the ownership and only dispose of the supplied connection if you own it.

    That was a lot of text, so let me summarize:

    1. Objects you own, you need to dispose of.
    2. Objects you don't, you do not dispose.

    There's also a third case, which it doesn't sound like you're having, but nonetheless.

    In the case when you construct, use, and discard, an object locally, inside a single method, without passing it around or storing it in fields of the class, you use the using statement instead, like this:

    using (IDbConnection conn = ....())
    {
    }
    

提交回复
热议问题