Click here to Skip to main content
15,891,248 members
Articles / Programming Languages / Visual Basic

Transactional Objects in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.14/5 (6 votes)
18 Oct 20044 min read 70.4K   776   24  
An article on how to easily return objects to their prior state after editing.
'***Header************************************************************************************
'
'       Name        -  
'       Description - 
'
'       Date        -  
'       Author      -  
'
'       Modification Log
'       -------------------------------------------------------------------------------
'       ModDate     -
'       ModBy       -
'       ModDesc     -
'
'********************************************************************************************

Public Class TransClass
    Implements Transaction.TransBase


    Public PubVar As String = ""
    Public PubCol As New Transaction.TransactionCollection

    Private privVar As String = ""
    Private m_bSetCalled As Boolean = False
    Private m_bGetCalled As Boolean = False

    <Transaction.Transactional(False)> Private m_oTrans As Transaction.DeepTrans
    Private m_bSet As Boolean = False
    Private m_bTransactional As Boolean

    Public ReadOnly Property SetCalled() As Boolean
        Get
            Return m_bSetCalled
        End Get
    End Property
    Public ReadOnly Property GetCalled() As Boolean
        Get
            Return m_bGetCalled

        End Get
    End Property

    Public Property Value() As String
        Get
            m_bGetCalled = True
            Return privVar
        End Get
        Set(ByVal Value As String)
            m_bSetCalled = True
            privVar = Value
        End Set
    End Property

    Public Sub Reset()
        m_bSetCalled = False
        m_bGetCalled = False
    End Sub


    Public Function BeginTrans() As Object Implements Transaction.TransBase.BeginTrans
        m_oTrans = New Transaction.DeepTrans
        m_oTrans.BeginTrans(Me, New TransClass)
        Return Me
    End Function

    Public Sub Commit() Implements Transaction.TransBase.Commit
        m_oTrans.Commit()
        m_oTrans = Nothing
    End Sub


    Public Sub ResetTransactional() Implements Transaction.TransBase.ResetTransactional
        m_bSet = False
    End Sub

    Public Function Rollback() As Object Implements Transaction.TransBase.Rollback
        m_oTrans.RollbackTrans()
        m_oTrans = Nothing
        Return Me
    End Function


    Public Sub SetTransactionality(ByVal State As Boolean) Implements Transaction.TransBase.SetTransactionality
        m_bSet = True
        m_bTransactional = State
    End Sub

    Public Function Transactional() As Boolean Implements Transaction.TransBase.Transactional
        Dim taAtt As Transaction.TransactionalAttribute
        Dim bReturn As Boolean

        If Not m_bSet Then
            taAtt = Attribute.GetCustomAttribute(Me.GetType, GetType(Transaction.TransactionalAttribute))
            If Not taAtt Is Nothing Then
                bReturn = taAtt.Transactional
            Else
                bReturn = True
            End If
        Else
            Return m_bTransactional
        End If

        Return bReturn
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions