posted on Friday, March 03, 2006 1:40 AM
by
Obi
IsDirty Implementation
Even though, there are a lot of frameworks in the market place to create distributed, object-oriented applications that employ .Net technologies e.g. CSLA and such. However, to implement such frameworks require that you have a good understanding of their hierarchical structure, base class libraries, dependencies and the like...
In my recent project, I was asked to quickly implement the Is Dirty functionality. As the name suggests, 'Is Dirty' is a term used, typically for data entry specific applications such as OLTP. When the user modifies a value in a text field or a combobox, check box etc. the application flags it as Dirty. If nothing changes, then making a trip to the database is not only useless but also costly.
So, here is how this seemingly simple Is Dirty implementation is used in a WinForm project:
'declare two module level variables of ArrayList. (ArrayList is a member of the System.Collections and implements the Ilist interface)
Private arrPrev, arrCurr As New ArrayList
Private Sub CustomerDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'some code goes here
SetState(Me, arrPrev)
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
SetState(Me, arrCurr)
Dim retValue As Boolean = IsDirty(arrPrev, arrCurr)
If Not retValue Then
Me.Cursor = Cursors.Default
MessageBox.Show("You have not made any changes", "MyApp", MessageBoxButtons.OK)
Me.Close()
Exit Sub
End If
End Sub
Friend Function IsDirty(ByVal arrPrev As ArrayList, ByVal arrCurr As ArrayList) As Boolean
Try
Dim i As Int16
For i = 0 To arrCurr.Count - 1
If arrCurr.Item(i).Equals(arrPrev.Item(i)) Then
'do nothing
Else
Return True
Exit For
End If
Next
Return False
Catch ex As Exception
Throw New ArgumentException("Is Dirty Error")
Finally
arrCurr.Clear()
End Try
End Function
Friend Sub SetState(ByVal frm As Form, ByVal arrState As ArrayList)
Try
Select Case frm.Name
Case NEW_CUSTOMER 'this is a constant for a form name
NewCustomerState(arrState)
'code for the other form(s) goes here
End Select
End Sub
Private Sub NewCustomerState(ByVal arrState As ArrayList)
Try
With NewCustomer
arrState.Add(.txtCustName.Text)
arrState.Add(.txtAppRecvdDate.Text)
arrState.Add(.txtCustAddress.Text)
arrState.Add(.txtCustCity.Text)
End With
Catch ex As Exception
Throw New ArgumentException("New Customer State could not be initialized")
End Try
End Sub
Cheers,
Obi Oberoi