Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing IDisposable on a simple class

Tags:

vb.net

I wanted to make a helper class for my MySql wrapper. The idea is that the methods that encapsulate mysql command construction and execution have an optional MySqlConnection as an argument. If a specific connection is passed, it uses that, if not, it creates one and disposes of it once done. To save 4 lines off every method, I could use this class in the using block and pass the optional argument as a construction parameter. Anyway, heres the class:

Public Class DynaConnection
    Implements IDisposable

    Public Dynamic As Boolean
    Public Connection As MySqlConnection
    Public Sub New(Connection As MySqlConnection)
        If Connection Is Nothing Then
            Dynamic = True
            Me.Connection = Connect()
        Else
            Dynamic = False
        End If
    End Sub
    Public Shared Widening Operator CType(ByVal Connection As DynaConnection) As MySqlConnection
        Return Connection.Connection
    End Operator

    Public Sub Dispose() Implements IDisposable.Dispose
        If Dynamic Then
            Connection.Close()
            Connection.Dispose()
        End If
        GC.SuppressFinalize(Me)
    End Sub
End Class

When I first wrote the letters "Implements IDisposable" though, a whole wall of code jumped into the class. I looked at msdn to see whats what but over there was an even longer bunch of code on how to "properly" implement IDisposable.

From what I remember from writing simple IDisposable classes before, what I've done in the class above should suffice. Has something changed?

like image 273
user81993 Avatar asked Sep 11 '26 10:09

user81993


1 Answers

This is that "wall of code" with some additional comments.

' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).

            'If your class holds references to other .NET objects 
            'that themselves implement IDisposable then you should implement IDisposable 
            'and call their Dispose method in yours

        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.

        'If you're holding any OS resources, e.g. file or image handles, 
        'then you should release them. That will be a rare thing for most people and can be pretty much ignored

        ' TODO: set large fields to null.
        'If any of your fields may refer to objects that occupy 
        'a large amount of memory then those fields should be set to Nothing

    End If
    Me.disposedValue = True
End Sub

' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
'    Dispose(False)
'    MyBase.Finalize()
'End Sub

' 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(disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub

I agree with GSeng, this is the correct way to implement IDisposable.

like image 174
dbasnett Avatar answered Sep 13 '26 05:09

dbasnett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!