Handling IDisposable in failed initializer or constructor

前端 未结 5 531
名媛妹妹
名媛妹妹 2020-12-11 06:34

Is there any nice pattern in .Net for ensuring that IDisposable fields owned by an object will get disposed if an exception is thrown during construction, possi

5条回答
  •  星月不相逢
    2020-12-11 07:07

    I've come up with a pattern that seems pretty good. It's inspired by an someone posted on CodeProject.com--using a list to keep track of disposables; raiiBase(of T) is a base class suitable for any class whose constructor takes a single parameter. The class constructor must be protected, and construction must be done via factory method. The static makeRaii() constructor takes a delegate to a factory function, which must accept a Stack(of iDisposable) along with a parameter of the class's expected type. A sample usage:

    Public Class RaiiTest
        Inherits raiiBase(Of String)
        Dim thing1 As testDisposable = RAII(New testDisposable("Moe " & creationParam, "a"))
        Dim thing2 As testDisposable = RAII(New testDisposable("Larry " & creationParam, "b"))
        Dim thing3 As testDisposable = RAII(New testDisposable("Shemp " & creationParam, "c"))
        Dim thing4 As testDisposable = RAII(New testDisposable("Curly " & creationParam, "d"))
    
        Protected Sub New(ByVal dispList As Stack(Of IDisposable), ByVal newName As String)
            MyBase.New(dispList, newName)
        End Sub
    
        Private Shared Function _newRaiiTest(ByVal dispList As Stack(Of IDisposable), ByVal theName As String) As RaiiTest
            Return New RaiiTest(dispList, theName)
        End Function
    
        Public Shared Function newRaiiTest(ByVal theName As String) As RaiiTest
            Return makeRaii(Of RaiiTest)(AddressOf _newRaiiTest, theName)
        End Function
    
        Shared Sub test(ByVal st As String)
            Try
                Using it As RaiiTest = newRaiiTest(st)
                    Debug.Print("Now using object")
                End Using
                Debug.Print("No exceptions thrown")
            Catch ex As raiiException
                Debug.Print("Output exception: " & ex.Message)
                If ex.InnerException IsNot Nothing Then Debug.Print("Inner exception: " & ex.InnerException.Message)
                For Each exx As Exception In ex.DisposalExceptions
                    Debug.Print("Disposal exception: " & exx.Message)
                Next
            Catch ex As Exception
                Debug.Print("Misc. exception: " & ex.Message)
            End Try
        End Sub
    End Class
    

    Since raiiTest inherits raiiBase(of String), to create a class instance, call newRaiiTest with a string parameter. RAII() is a generic function that will register its argument as an iDisposable that will need cleaning up, and then return it. All registered disposables will be Disposed when either Dispose is called on the main object, or when an exception is thrown in the construction of the main object.

    Here's the riaaBase class:

    Option Strict On
    Class raiiException
        Inherits Exception
        ReadOnly _DisposalExceptions() As Exception
        Sub New(ByVal message As String, ByVal InnerException As Exception, ByVal allInnerExceptions As Exception())
            MyBase.New(message, InnerException)
            _DisposalExceptions = allInnerExceptions
        End Sub
        Public Overridable ReadOnly Property DisposalExceptions() As Exception()
            Get
                Return _DisposalExceptions
            End Get
        End Property
    End Class
    
    Public Class raiiBase(Of T)
        Implements IDisposable
    
        Protected raiiList As Stack(Of IDisposable)
        Protected creationParam As T
        Delegate Function raiiFactory(Of TT As raiiBase(Of T))(ByVal theList As Stack(Of IDisposable), ByVal theParam As T) As TT
    
        Shared Function CopyFirstParamToSecondAndReturnFalse(Of TT)(ByVal P1 As TT, ByRef P2 As TT) As Boolean
            P2 = P1
            Return False
        End Function
    
        Shared Function makeRaii(Of TT As raiiBase(Of T))(ByVal theFactory As raiiFactory(Of TT), ByVal theParam As T) As TT
            Dim dispList As New Stack(Of IDisposable)
            Dim constructionFailureException As Exception = Nothing
            Try
                Return theFactory(dispList, theParam)
            Catch ex As Exception When CopyFirstParamToSecondAndReturnFalse(ex, constructionFailureException)
                ' The above statement let us find out what exception occurred without having to catch and rethrow
                Throw ' Should never happen, since we should have returned false above
            Finally
                If constructionFailureException IsNot Nothing Then
                    zapList(dispList, constructionFailureException)
                End If
            End Try
        End Function
    
        Protected Sub New(ByVal DispList As Stack(Of IDisposable), ByVal Params As T)
            Me.raiiList = DispList
            Me.creationParam = Params
        End Sub
    
        Public Shared Sub zapList(ByVal dispList As IEnumerable(Of IDisposable), ByVal triggerEx As Exception)
            Using theEnum As IEnumerator(Of IDisposable) = dispList.GetEnumerator
                Try
                    While theEnum.MoveNext
                        theEnum.Current.Dispose()
                    End While
                Catch ex As Exception
                    Dim exList As New List(Of Exception)
                    exList.Add(ex)
                    While theEnum.MoveNext
                        Try
                            theEnum.Current.Dispose()
                        Catch ex2 As Exception
                            exList.Add(ex2)
                        End Try
                    End While
                    Throw New raiiException("RAII failure", triggerEx, exList.ToArray)
                End Try
            End Using
        End Sub
    
        Function RAII(Of U As IDisposable)(ByVal Thing As U) As U
            raiiList.Push(Thing)
            Return Thing
        End Function
    
        Shared Sub zap(ByVal Thing As IDisposable)
            If Thing IsNot Nothing Then Thing.Dispose()
        End Sub
    
        Private raiiBaseDisposeFlag As Integer = 0 ' To detect redundant calls
    
        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso Threading.Interlocked.Exchange(raiiBaseDisposeFlag, 1) = 0 Then
                zapList(raiiList, Nothing)
            End If
        End Sub
    
    #Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
    #End Region
    
    End Class
    

    Note that a custom exception type will be thrown if disposal fails for any or all of the registered disposable objects. InnerException will indicate whether the constructor failed; to see which disposer(s) failed, check DisposalExceptions.

提交回复
热议问题