|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionEver had to return an object to its original state after a user has clicked the Cancel button? I am sure that most of us have, and we have had to write a whole series of special methods to do this, methods that are only valid for that object, and methods that fail when later on we add more properties to the object and forget to modify the 'Transaction' routines. With the Transaction code that this article covers, simple yet powerful transactions can be easily added to existing and new projects. BackgroundOn a boring Saturday, I decided to see what VB.NET had to offer that might allow me to dynamically do object transactions. After some searching through MSDN, I came across the Then I decided that I wanted to be able to control what members of a class would enter into a transaction, and thus I came across the functionality provided by the And finally, since I found that, when making a 'copy' of an object in a transaction, all that was copied was a reference, and since many classes use collections, I created a weakly typed Collection object that could work in transactions. Apart from the source code, a demo application DemoTrans is provided that demonstrates some of the basic functionality provided by the application. Using the codeThe Transaction application provides two transactional classes ( Each of the provided classes include comments that try and describe how they work and how they can be used. What follows is a brief overview of these classes.
To begin to use the Transaction Application with an existing object, we can simply use the following code: Private Sub DoTransaction()
Dim oTrans as New Transaction.DeepTrans
'Or Shallow if we want to only access Properties
'Assumes we have an Object defined as MyObject of Type MyClass
oTrans.BeginTrans(MyObject, New MyClass)
'Code that modifies the Object
If bEditCanceled = True Then
'Object Variables are returned to their original state
oTrans.Rollback()
Else
'Keep the changes
oTrans.Commit()
End If
End Sub
Now if any of the variables of the object were themselves objects and changes were made to them, these changes were not rolled back. To be able to do this, these objects must implement the The code for running a Transaction over a class that implements the Private Sub DoTransaction()
'Assumes we have an Object defined as MyObject whose class implements TransBase
MyObject.BeginTrans
'Code that modifies the Object
If bEditCanceled = True Then
'Object Variables are returned to their original state
MyObject.RollbackTrans()
Else
'Keep the changes
MyObject.Commit()
End If
End Sub
We can control whether a class, a property, or a variable will be covered by a transaction, by setting a custom attribute using the Attribute class provided, 'Flags a class as transactional (default)
<Transaction.Transactional(True)>Public Class MyObject
' Flags a class as not transactional
<Transaction.Transactional(False)>Public Class MyObject
'Flags a variable as transactional (default)
<Transaction.Transactional(True)>Private MyVar As Integer
'Flags a variable as not transactional
<Transaction.Transactional(False)>Private MyVar As Integer
'Flags a Property as transactional (default)
<Transaction.Transactional(True)>Public Property MyProp() As Integer
'Flags a Property as not transactional
<Transaction.Transactional(False)>Public Property MyProp() As Integer
Additionally, classes that implement the
As I mentioned above, one issue I came across was the fact that even at this level, when treating objects, what was copied was a reference. Since many classes that need this type of functionality use collections or collection based members, this was a problem. So, using the While this collection is weakly typed, it can easily be inherited to provide strongly typed collection objects. Comments in the code explain what needs to be done with this. Points of InterestWriting this code was a blast ... I feel like I have only touched the tip of the iceberg where it comes to the functionality that VB.NET exposes. Attributes and the HistoryThis first version (18 Oct 2004) has been more or less tested, but has yet to used in a real-life application ....
|
|||||||||||||||||||||||||||||||||||||||||||||||||||