Original value tracking

Original value tracking

Old forum URL: forums.lhotka.net/forums/t/5056.aspx


JoeFallon1 posted on Thursday, July 03, 2008

I am trying to implement the original value tracking that was discussed in this thread:
http://forums.lhotka.net/forums/5/19925/ShowThread.aspx

But I am having a bit of trouble.

1. When DataPortal_Fetch is called the defaultValue is first set and THEN the value from the database is set. So it takes 2 passes to get the correct original value.

2. But when DataPortal_Create is called, the default Value could be the Original value unless the BO developer sets it to something else. So in this case it is either 1 or 2 passes depending on how the BO is configured. The code below is not reliable enough to handle both cases.

Patrick's idea was to have the PropertyInfo object passed in to the constructor of the FieldData class *along with the Original Value*. But where did the PropertyInfo class get the Original value??

So I guess my question is: Is there a better way to track the Original value?

<Serializable()> _
Public Class MyFieldData(Of T)
 
Inherits FieldData(Of T)
 
Implements IFieldData(Of T)

  Private _origdata As T
 
Private _hasBeenSet As Integer = 0
 
Private _trackOrigValue As Boolean = False

  Public Sub New(ByVal name As String, Optional ByVal trackOrigValue As Boolean = False)
   
MyBase.New(name)
    _trackOrigValue = trackOrigValue
 
End Sub

  Public Overrides Property Value() As T
   
Get
     
Return MyBase.Value
   
End Get

    Set(ByVal value As T)
     
MyBase.Value = value 'sets IsDirty = True

      'When a BO is Fetched from the DB, the framework sets the DefaultValue first and THEN sets the DB value.
     
'So the first time it is set is not the correct time to flip the flag!
     
'What about when DP_Create is called? Then setting the default value is the correct place to set the flag!
     
If _trackOrigValue AndAlso _hasBeenSet <= 1 Then 'only set the original value once.
       
_origdata = value
        _hasBeenSet += 1
     
End If

      If _trackOrigValue AndAlso MyBase.Value.Equals(_origdata) Then
       
MarkClean() 'sets IsDirty to False
     
End If
   
End Set
 
End Property

  Public ReadOnly Property OrigValue() As T
   
Get
     
Return _origdata
   
End Get
 
End Property

  Public Overrides ReadOnly Property IsDirty() As Boolean
   
Get
     
If _trackOrigValue AndAlso MyBase.Value.Equals(_origdata) Then
       
Return False
     
Else
       
Return MyBase.IsDirty
     
End If
   
End Get
 
End Property

End Class

Joe

Copyright (c) Marimer LLC